ActiveRecord has_one 关系在某些情况下不会返回
给定三个模型,每个模型都相互嵌套。如果我创建顶级对象并 build_* 其他子对象,我可以通过原始实例上 save() 之前和之后的关系检索所有子对象。但是,如果我尝试在 find(:id) 原始父对象之后检索第二级嵌套对象,则会失败。但是,我可以检索第一级嵌套对象。这通常发生在控制器中,但我将在下面的控制台输出中进行说明。
我在忽略什么?
预先感谢!
~jpv
>> l = Lead.new :buyer_name => 'Kim Possible', :email => '[email protected]', :phone => '7131231234' >> l.build_buyer >> l.buyer.name = 'kim buyer' >> l.buyer >> l.buyer.build_contact_detail >> l.buyer.contact_detail.email = "[email protected]" >> l.save #returns true >> l.buyer #THIS WORKS => #<Buyer id: 1, name: "kim buyer", lead_id: 1> >> l.buyer.contact_detail #THIS WORKS => #<ContactDetail id: 1, company_id: nil, buyer_id: 1, email: nil, address_line_1: nil, address_line_2: nil, city: nil, state: nil, postal_code: nil> >> l2 = Lead.find(1) => #<Lead id: 1, company_id: nil, buyer_id: nil, public_lead_id: nil, buyer_name: "Kim Possible", company_name: nil, email: "[email protected]", phone: "7131231234"> >> l2.buyer #THIS WORKS AS EXPECTED => #<Buyer id: 1, name: "kim buyer", lead_id: 1> >> l2.buyer.contact_detail #THIS BREAKS => nil
下面的所有样板内容:
class Lead has_one :buyer #... end class Buyer has_one :contact_detail belongs_to :lead #... end class ContactDetail belongs_to :buyer #... end
适当的外键位于每个“belongs_to”类中。
class CreateBuyers < ActiveRecord::Migration def self.up create_table :buyers do |t| t.string :name t.integer :lead_id ... class CreateContactDetails < ActiveRecord::Migration def self.up create_table :contact_details do |t| t.integer :buyer_id
Given three models that are each nested in each other. If I create the top-level object and build_* the other child objects, I can retrieve all child objects through the relationships before and after save() on the original instance. However, if I try to retrieve the 2nd level nested object after find(:id) the original parent it fails. I can retrieve the 1st level nested object, however. This usually happens in a controller, but I'll illustrate it in console output below.
What am I overlooking?
Thanks in advance!
~jpv
>> l = Lead.new :buyer_name => 'Kim Possible', :email => '[email protected]', :phone => '7131231234' >> l.build_buyer >> l.buyer.name = 'kim buyer' >> l.buyer >> l.buyer.build_contact_detail >> l.buyer.contact_detail.email = "[email protected]" >> l.save #returns true >> l.buyer #THIS WORKS => #<Buyer id: 1, name: "kim buyer", lead_id: 1> >> l.buyer.contact_detail #THIS WORKS => #<ContactDetail id: 1, company_id: nil, buyer_id: 1, email: nil, address_line_1: nil, address_line_2: nil, city: nil, state: nil, postal_code: nil> >> l2 = Lead.find(1) => #<Lead id: 1, company_id: nil, buyer_id: nil, public_lead_id: nil, buyer_name: "Kim Possible", company_name: nil, email: "[email protected]", phone: "7131231234"> >> l2.buyer #THIS WORKS AS EXPECTED => #<Buyer id: 1, name: "kim buyer", lead_id: 1> >> l2.buyer.contact_detail #THIS BREAKS => nil
All the boilerplate stuff below:
class Lead has_one :buyer #... end class Buyer has_one :contact_detail belongs_to :lead #... end class ContactDetail belongs_to :buyer #... end
The appropriate foreign keys are in each of the "belongs_to" classes.
class CreateBuyers < ActiveRecord::Migration def self.up create_table :buyers do |t| t.string :name t.integer :lead_id ... class CreateContactDetails < ActiveRecord::Migration def self.up create_table :contact_details do |t| t.integer :buyer_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可能会被绊倒,因为您期望对象急切加载嵌套的子对象。一般来说,您必须明确指定它才能在查找中发生。尝试添加 :include => [:借款人,{:借款人=> :contact_detail}] 到查找选项并查看是否有效。
I think you might be getting tripped up because you're expecting the object to eager load nested child objects. In general, you have to explicitly specify it for that to happen in the find. Try adding :include => [:borrower, {:borrower => :contact_detail}] to the options for the find and see if that works.
如果你这样做,这是否有效...
如果是这样,这可能是一个 Rails 错误,ContactDetail 对象在创建时并不真正知道它的父亲是谁。
Does this work if you do...
If so, this could be a rails bug with the ContactDetail object not really knowing who its daddy is at creation time.