关于 rails ActiveRecord 属性 以及 foreign_key 不直接用数据库项目 时的一些讨论
2008-03-29 10:14
399 查看
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)时变量也不被设置
于是查看了ActiveRecord的源代码,:foreign_key实际读写的是x.attributes[name]项
也就是说自己定义的读取方法并不属于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会出错
也就是说如果不调用读写方法,x.attributes[major_id] == nil,而foreign_key直接调用x.attributes[major_id]
只有调用钩子
def after_initialize
self[:major_id] =
end
这样就可以正常读取,写入
但对于写入方法,建议是在before_save钩子中完成,而不是使用覆盖属性读写的方法
但是数据库中并不直接有major_id这一项,而是从其他项表出[比如从一个xml项中解析出来]
最先想到的很简单
def major_id
end
def major_id=(m)
end
觉得这样实现就可以了,但是现实是残酷的
调用x.major时返回nil ,设置x.major = Major.find(1)时变量也不被设置
于是查看了ActiveRecord的源代码,:foreign_key实际读写的是x.attributes[name]项
也就是说自己定义的读取方法并不属于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会出错
也就是说如果不调用读写方法,x.attributes[major_id] == nil,而foreign_key直接调用x.attributes[major_id]
只有调用钩子
def after_initialize
self[:major_id] =
end
这样就可以正常读取,写入
但对于写入方法,建议是在before_save钩子中完成,而不是使用覆盖属性读写的方法
相关文章推荐
- 关于过去工作的一些总结:常用sql语句以及数据库查询优化
- 尽管关于Java中文问题的讨论已经相当多了,但由于Java的相关技术标准繁多,面向Java的Web服务器、应用服务器以及JDBC数据库驱动等都没有官方的标准,所以Java应用在处理中文时所存在的问
- 关于maven项目初始化数据库的一些经验
- iOS小项目中的一些小拾遗(关于UIColor颜色以及UIButton制作圆形按钮)
- 关于Ruby, Ruby on Rails的一些概念属性的介绍
- python 初体验,以及关于python项目的一些设想
- 关于对象数据库 DB4O 的一些BUG以及如何应对的方法
- iOS开发设置关于tabBar和navigationBar以及item中的一些全局属性
- Rails与数据库查询到关于datetime的记录不一致引发的讨论
- 关于img标签的相对路径和绝对路径以及a标签的一些属性问题
- 20130910 一些想法,关于项目中异常处理的解决方案,以及Elmah
- 关于MySql中数据库、表的操作以及增删改查等一些SQL语句注意事项
- Rails 中 ActiveRecord 映射数据库属性的问题
- 关于继承的讨论,以及私有属性继承的问题
- 关于数据库的操作以及一些理解
- 关于字符串判断的几个常用属性 以及简单项目实现中数组运用的初步思想 蓝懿教育
- 深夜的讨论——关于团队分工,以及我想到了一些关于国企的事情
- 地磅称量系统之(37~39) 直接向数据库的数据表WtBill添加测试数据以及绑定用户控件和使用编码的方式控制dataGridView控件的每列属性和添加数据数据库的表中不存在的字段(非绑定列)
- 关于AndroidStudio如何导入新项目LibraryProject以及遇到的一些问题