has_one :通过连接模型

发布于 2024-08-18 13:02:29 字数 730 浏览 4 评论 0原文

我想构建一些东西,以便一个人可以拥有多个电子邮件地址,而一个电子邮件地址只能包含一个人,但是因为我还有另一个名为 Company 的模型,它也可以拥有多个电子邮件地址,并且我不想拥有 company_id 列和电子邮件表中的 person_id ,所以我想我可以做...

person.rb

has_many :person_emails has_many :电子邮件, :通过 => :person_emails

person_emails.rb

属于:person 属于:电子邮件

email.rb

has_one :person_email has_one :person, :through =>; :person_email

现在发生的事情是...

p = Person.first #=> “尼克” p.emails#=>显示 Nik 的所有电子邮件 p.person_emails #=> 的所有 person_email 联表记录

显示 Nik e = Email.first #=> Nik 的电子邮件地址之一 e.person_email #=>显示此电子邮件的唯一一条 person_email 联表记录 where 子句中说出未知列“people.email_id”

e.person # 未能在我想要的 ... e.person #=> “尼克”

有人知道问题出在哪里吗?

谢谢

I want to build something so that a person can have many email addresses and an email address has only one person, but because I also have another model called Company that also can have many email addresses, and I don't want to have columns company_id and person_id in the Emails table, so I thought I can do ...

person.rb

has_many :person_emails
has_many :emails, :through => :person_emails

person_emails.rb

belongs_to :person
belongs_to :email

email.rb

has_one :person_email
has_one :person, :through => :person_email

What's happening now is that...

p = Person.first #=> "Nik"
p.emails #=> shows all emails Nik has
p.person_emails #=> shows all person_email joint table records for Nik

e = Email.first #=> one of Nik's email addresses
e.person_email #=> shows this email's one and only one person_email joint table record
e.person # fails saying that unknown column "people.email_id" in where-clause

I'd like...
e.person #=> "Nik"

Does anyone have an idea what the problem might be?

Thank You

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

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

发布评论

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

评论(1

一桥轻雨一伞开 2024-08-25 13:02:29

您的情况建议使用多态关联,它比 has_many :through 关联干净得多。例如:

class Person
  has_many :emails, :as => :emailable
end

class Company
  has_many :emails, :as => :emailable
end

class Email
  belongs_to :emailable, :polymorphic => true
end

您可以完全删除 PersonEmails 类。在数据库中,您的 emails 表将如下所示:

create_table :emails do |t|
  t.string :address
  t.string :emailable_type
  t.integer :emailable_id
end

emailable_type 列存储关联模型的名称,在您的情况下为 “Person”"Company"emailable_id 存储关联对象的 ID。有关详细信息,请参阅“多态关联”下的Rails API 文档

Your situation suggests using polymorphic associations, which are much cleaner than has_many :through associations. For example:

class Person
  has_many :emails, :as => :emailable
end

class Company
  has_many :emails, :as => :emailable
end

class Email
  belongs_to :emailable, :polymorphic => true
end

You can get rid of your PersonEmails class entirely. In the database, your emails table will look something like this:

create_table :emails do |t|
  t.string :address
  t.string :emailable_type
  t.integer :emailable_id
end

The emailable_type column stores the name of the associated model, in your case "Person" or "Company", and the emailable_id stores the id of the associated object. For more information see the Rails API documentation under "Polymorphic Associations".

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