导轨的ActiveRecord处理一个ID列不是主键

导轨的ActiveRecord处理一个ID列不是主键

本文介绍了导轨的ActiveRecord处理一个ID列不是主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与ActiveRecord的自动分配的挣扎:id属性的,即使它是一个单独的列的主键。

Struggling with ActiveRecord auto assigning the :id attribute as the primary key even though it is a separate column.

Table - legacy-table

    id - int
    pk_id - int (primary key)
    name - varchar2
    info - varchar2

型号

class LegacyModel < ActiveRecord::Base
  self.table_name = 'legacy-table'
  self.primary_key = 'pk_id'
  default_scope {order(:name => :asc)}
  alias_attribute :other_id, :id

end

我不关心ActiveRecord的自动分配的主键(pk_id)到:但是,我失去所有进入实际ID列id属性。尝试使用别名只是指向我回到了主键。

I don't care that ActiveRecord automatically assigns the primary key (pk_id) to the :id attribute however I lose all access to the actual id column. Trying to use the alias simply points me back at the primary key.

不过注意,该问题是,从该视图我可以使用@legacymodel访问ID列[:ID]。但是,再次调用@ legacymodel.id时,我得到了pk_id列的值。我想要的是能够调用@ legacymodel.other_id并使其指向id列。相反@ legacymodel.service_id,@ legacymodel.id,和@ legacymodel.pk_id都指向同一列pk_id

However one caveat to this issues is that from the view i can access the id column by using @legacymodel[:id]. But again when calling @legacymodel.id I get the value of the pk_id column. What i want is to be able to call @legacymodel.other_id and have it point to the id column. Instead @legacymodel.service_id, @legacymodel.id, and @legacymodel.pk_id all point to the same column pk_id

请注意,这是一个传统的数据库,并修改列是出了问题。我使用Rails 4的MySQL。

Please note that this is a legacy db and modifying the columns are out of the question. I am using Rails 4 with MySql.

反正有没有解决这个code?为什么@legacymodel [:ID]给我不同的结果,然后@ legacymodel.id?

Is there anyway to code around this? Why does @legacymodel[:id] give me different results then @legacymodel.id?

推荐答案

read_attribute 方法将读取值了 @属性散的。该 [] 方法使用 read_attribute 。因此, @legacymodel [:ID] 得到 id的值

The read_attribute method will read a value out of the @attributes hash. The [] method uses read_attribute. So @legacymodel[:id] gets the value of the id column.

write_attribute 方法总是试图翻译 ID 到主键的名称......

The write_attribute method always tries to translate id into the name of the primary key...

# ActiveRecord::AttributeMethods::Write
def write_attribute(attr_name, value)
  attr_name = attr_name.to_s
  attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key

...和 [] = 方法使用 write_attribute 。因此, @legacymodel [:ID =&LT;价值&GT; 将值设置到主键列, pk_id

...and the []= method uses write_attribute. So @legacymodel[:id] = <value> will set a value into the primary key column, pk_id.

ID 方法是别名 primary_key 这里的特殊方法:

The id method is a special method that is aliased to the primary_key here:

# ActiveRecord::AttributeMethods::PrimaryKey
if attr_name == primary_key && attr_name != 'id'
  generated_attribute_methods.send(:alias_method, :id, primary_key)
end

所以 @ legacymodel.id 将获得 pk_id 列的值。

如果你只想读 ID @ legacymodel.other_id ,那么你可以定义一个方法列这样的:

If you just want to read the id column through @legacymodel.other_id, then you could define a method like:

# LegacyModel class
def other_id
  self[:id]
end

但是,如果你还需要通过 @ legacymodel.other_id = 写入 ID 列,那么你可能需要设法找到一种安全的方式来覆盖 write_attribute 方法,这样就可以解决的名称= self.class.primary_key如果attr_name == '身份证'和;&安培; self.class.primary_key 语句。

But if you also need to write to the id column through @legacymodel.other_id=, then you might need to try to find a safe way to override the write_attribute method so that you can work around the attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key statement.

这篇关于导轨的ActiveRecord处理一个ID列不是主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:28