是否有更有效的方法来在通过另一个表引用自身的类上创建 ActiveRecord 关系?

发布于 2024-11-09 16:32:12 字数 1890 浏览 0 评论 0原文

如果问题标题令人困惑,我深表歉意。我有以下情况:我有一个 Person 模型,它以标准方式将人员存储在 people 表中。

我需要在 Person 模型上为 emergency_contacts 添加 has_many 关系。我尝试通过以下方式执行此操作:

迁移:

create_table :people do |t|
  t.string :first
  t.string :middle
  t.string :last

  t.timestamps
end
create_table :emergency_contacts, :id => false do |t|
  t.integer :person_id
  t.integer :emergency_contact_id

  t.timestamps
end

模型:

class Person < ActiveRecord::Base
  has_many :emergency_contacts

  validates :first, :presence => true
  validates :last, :presence => true
end
class EmergencyContact < ActiveRecord::Base
  belongs_to :person
  has_one :person, :foreign_key => 'emergency_contact_id'
end

这允许我执行以下操作:

ruby-1.9.2-p180 :001 > p = Person.new(first: "John", last: "Doe")
 => #<Person id: nil, first: "John", middle: nil, last: "Doe", created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :002 > ec = EmergencyContact.new
 => #<EmergencyContact person_id: nil, emergency_contact_id: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :003 > ec.emergency_contact = Person.new(first: "Peter", last: "Griffin")
 => #<Person id: nil, first: "Peter", middle: nil, last: "Griffin", created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :004 > p.emergency_contacts << ec
 => [#<EmergencyContact person_id: nil, emergency_contact_id: nil, created_at: nil, updated_at: nil>] 
ruby-1.9.2-p180 :005 > p.save!
 => true 

但是,我认为 EmergencyContact 模型不必在那里,因为我实际上只是引用 <无论如何,代码>人模型。

有没有办法删除这个“中间人”模型,这样我就可以做类似的事情:

p = Person.new(first: "John", last: "Doe")
p.emergency_contacts << Person.new(first: "Peter", last: "Griffin")

I apologize if the question title is confusing. I have the following situation: I have a Person model which stores people in the standard way in the people table.

I need to add a has_many relationship for emergency_contacts on the Person model. I tried doing this in the following way:

Migrations:

create_table :people do |t|
  t.string :first
  t.string :middle
  t.string :last

  t.timestamps
end
create_table :emergency_contacts, :id => false do |t|
  t.integer :person_id
  t.integer :emergency_contact_id

  t.timestamps
end

Models:

class Person < ActiveRecord::Base
  has_many :emergency_contacts

  validates :first, :presence => true
  validates :last, :presence => true
end
class EmergencyContact < ActiveRecord::Base
  belongs_to :person
  has_one :person, :foreign_key => 'emergency_contact_id'
end

This allows me to do:

ruby-1.9.2-p180 :001 > p = Person.new(first: "John", last: "Doe")
 => #<Person id: nil, first: "John", middle: nil, last: "Doe", created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :002 > ec = EmergencyContact.new
 => #<EmergencyContact person_id: nil, emergency_contact_id: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :003 > ec.emergency_contact = Person.new(first: "Peter", last: "Griffin")
 => #<Person id: nil, first: "Peter", middle: nil, last: "Griffin", created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :004 > p.emergency_contacts << ec
 => [#<EmergencyContact person_id: nil, emergency_contact_id: nil, created_at: nil, updated_at: nil>] 
ruby-1.9.2-p180 :005 > p.save!
 => true 

However, I don't feel the EmergencyContact model should have to be there, since I am really just referencing a Person model anyway.

Is there a way to remove this "middle-man" model, so that I can just do something like:

p = Person.new(first: "John", last: "Doe")
p.emergency_contacts << Person.new(first: "Peter", last: "Griffin")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

听风念你 2024-11-16 16:32:12

我真的会使用自我参照关联来达到这种目的。

教程在这里: http://railscasts.com/episodes/163-self-referential-association

I'd really use self referential associations for this kind of purpose.

Se tutorial here: http://railscasts.com/episodes/163-self-referential-association

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