关于 rails ActiveRecord 属性 以及 foreign_key 不直接用数据库项目 时的一些讨论
引发这个问题的是 需要用类似于下面的功能
belongs_to :major,:foreign_key => :major_id
但是数据库中并不直接有major_id这一项,而是从其他项表出[比如从一个xml项中解析出来]
def major_id

end
def major_id=(m)

end
觉得这样实现就可以了,但是现实是残酷的调用x.major时返回nil ,设置x.major = Major.find(1)时变量也不被设置
也就是说自己定义的读取方法并不属于x.attributes
在API里查看到以下内容
Overwriting default accessors
All column values are automatically available through basic accessors on the Active Record object, but sometimes you want to specialize this behavior. This can be done by overwriting the default accessors (using the same name as the attribute) and calling read_attribute(attr_name) and write_attribute(attr_name, value) to actually change things. Example:
class Song < ActiveRecord::Base
# Uses an integer of seconds to hold the length of the song
def length=(minutes)
write_attribute(:length, minutes * 60)
end
def length
read_attribute(:length) / 60
end
end
You can alternatively use self[:attribute]=(value) and self[:attribute] instead of write_attribute(:attribute, value) and read_attribute(:attribute) as a shorter form. 如果设置x.major_id=...,可以使用x.major,一切正常
如果设置x.major,x.major_id也正常
但是
x = X.find(1)
x.major会出错只有调用钩子
def after_initialize
self[:major_id] = 
end
但对于写入方法,建议是在before_save钩子中完成,而不是使用覆盖属性读写的方法


浙公网安备 33010602011771号