ActiveRecord has_one 关系在某些情况下不会返回

发布于 2024-08-16 14:45:45 字数 2180 浏览 1 评论 0原文

给定三个模型,每个模型都相互嵌套。如果我创建顶级对象并 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请叫√我孤独 2024-08-23 14:45:45

我认为您可能会被绊倒,因为您期望对象急切加载嵌套的子对象。一般来说,您必须明确指定它才能在查找中发生。尝试添加 :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.

小巷里的女流氓 2024-08-23 14:45:45

如果你这样做,这是否有效...

l.buyer.name = 'kim buyer'
l.save
l.buyer.build_contact_detail
...

如果是这样,这可能是一个 Rails 错误,ContactDetail 对象在创建时并不真正知道它的父亲是谁。

Does this work if you do...

l.buyer.name = 'kim buyer'
l.save
l.buyer.build_contact_detail
...

If so, this could be a rails bug with the ContactDetail object not really knowing who its daddy is at creation time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文