惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - Tachikoma

用Ruby实现 Web Service Server ,并用Ruby发送 HTTP请求 Web Service Android 使用 Ksoap2 出现的低级错误... - Tachikoma Cygwin中通过RJB在Ruby下调用ICTCLAS(JAVA) ICTCLAS4J 的编译脚本 Ruby Sandbox 实现运行客户代码 - Tachikoma Ruby在使用MongoDB时,对Cursor的重新包装 ruby 下使用 ICTCLAS(JAVA) RJB 在windows下的一些安装事项 Ruby手工测试正确,rcov测试失败的解决 在dell dimension 5150 上安装 leopard 手记 Rails测试中清空数据表/载入空fixtures RDoc 解决同名module 与 Class的问题 C 实用的 e-editor 的bundle Best of Ruby Quiz - Animal Quiz Best of Ruby Quiz - GEDCOM Parser Best of Ruby Quiz - MadLib - Tachikoma 使用selenium-on-rails的一些讨论 3]assertXpathCount的使用 使用selenium-on-rails的一些讨论 2]清理缓存 - Tachikoma - 博客园 使用selenium-on-rails的一些讨论 [0,1]
关于 rails ActiveRecord 属性 以及 foreign_key 不直接用数据库项目 时的一些讨论
Tachikoma · 2008-03-29 · via 博客园 - Tachikoma


引发这个问题的是 需要用类似于下面的功能

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.find(1)
x.major会出错

也就是说如果不调用读写方法,x.attributes[major_id] == nil,而foreign_key直接调用x.attributes[major_id]
只有调用钩子

def after_initialize
  self[:major_id] 
= 
end

这样就可以正常读取,写入

但对于写入方法,建议是在before_save钩子中完成,而不是使用覆盖属性读写的方法