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

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

博客园 - 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钩子中完成,而不是使用覆盖属性读写的方法