为什么 ActiveRecord 不够聪明,无法知道父亲的 object_id 应该等于其孩子的父级的 object_id? [英] Why is ActiveRecord not smart enough to know that the object_id of the father should be equal to the object_id of the parent of its children?
本文介绍了为什么 ActiveRecord 不够聪明,无法知道父亲的 object_id 应该等于其孩子的父级的 object_id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@father = Hierarchy.find(:first, :conditions => ['label = ?', 'father'])
@father.children.each do |child|
puts @father.object_id == child.parent.object_id
end
我原以为这里的结果都是true
.
I would have thought the results here would be all true
.
相反,它们都是false
.
为什么 ActiveRecord 以这种方式工作,而不是识别出这些是相同的 Ruby 对象?
Why does ActiveRecord work this way instead of recognizing that these are the same Ruby objects?
推荐答案
对于那些在被问到这个问题后几年偶然发现的人(像我一样!),请查看 https://stackoverflow.com/a/4116397/1000655.
For anyone who stumbles across this years after it was asked (like me!), check out https://stackoverflow.com/a/4116397/1000655.
建议使用 :inverse_of
来建立双向关联.我真希望我过去没有浏览过那么多次文档!
It suggests using :inverse_of
to set up Bi-directional associations. I wish I hadn't skimmed that bit of the documentation so many times in the past!
这篇关于为什么 ActiveRecord 不够聪明,无法知道父亲的 object_id 应该等于其孩子的父级的 object_id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文