ruby-on-rails - ActiveRecord 与两个不同模型的关联

标签 ruby-on-rails ruby activerecord ruby-on-rails-5 polymorphic-associations

我很难思考应该如何配置表 + 关联。

我有一个 Lawsuit 模型。一场诉讼有_很多方(被告、原告、律师等)。一方又可以是 PersonCompany。最终,我希望能够得到:

  • 一个人的诉讼 (@person.lawsuits);
  • 一家公司的诉讼(@company.lawsuits);和
  • 诉讼的当事人 (@lawsuit.parties),可以是公司

这就是我目前设置表格和模型的方式:

| id | fname  | lname | date_of_birth |
| -- | ------ | ----- | ------------- |
|  1 | John   | Smith |    1974-02-04 |
|  2 | George | Glass |    1963-07-29 |

公司

| id | name      | duns      | ticker | address      |
| -- | --------- | --------- | ------ | ------------ |
|  1 | Acme Inc. | 239423243 | ACME   | 123 Main St. |

诉讼

| id | jurisdiction | court | case_no    | title                       |
| -- | ------------ | ----- | ---------- | --------------------------- |
|  1 |      federal | SDNY  | 18-CV-1234 | Smith v. Glass, Acme, et al |

诉讼当事人

| id | lawsuit_id | person_id | company_id | role      |
| -- | ---------- | --------- | ---------- | --------- |
|  1 |          1 |         1 |            | plaintiff |
|  2 |          1 |         2 |            | defendant |
|  3 |          1 |           |          1 | defendant |
# models/lawsuit.rb:
class Lawsuit < ApplicationRecord
    has_many :lawsuit_parties

    def parties
        self.lawsuit_parties
    end

    def defendants
        self.parties(where(lawsuit_parties: {role: 'defendant'})
    end

    def plaintiffs
        self.parties(where(lawsuit_parties: {role: 'plaintiff'})
    end

    def attorneys
        self.parties(where(lawsuit_parties: {role: 'attorney'})
    end
end
# models/lawsuit_party.rb
class LawsuitParty < ApplicationRecord
    belongs_to :person
    belongs_to :company
end
# models/person.rb
class Person < ApplicationRecord
    has_many :lawsuit_parties
    has_many :lawsuits, through: :lawsuit_parties
end
# models/company.rb
class Company < ApplicationRecord
    has_many :lawsuit_parties
    has_many :lawsuits, through: :lawsuit_parties
end

如有任何帮助,我们将不胜感激......

最佳答案

你走在正确的 rails 上,但你需要引入一个 polymorphic relationship到您的连接模型上以使这种类型的建模起作用。 An Enum can handle differentiating between Defendants and Plaintiffs ,并免费提供您要求的多个范围/方法。

class LawsuitParty < ApplicationRecord
    belongs_to :lawsuit
    belongs_to :partiable, polymorphic: true

    enum role: [:defendant, :plaintiff]
end

您需要编写迁移以将您的 lawsuit_parties 表更改为以下列(所有 Rails 约定名称):

partiable_id   = Integer
partiable_type = String
role           = String

lawsuit_parties

| id | lawsuit_id | partiable_id | partiable_type | role      | 
| -- | ---------- | ------------ | -------------- | ----------|
|  1 |          1 |            1 | Person         | defendant |
|  2 |          1 |            2 | Company        | plaintiff |
|  3 |          1 |            1 | Company        | defendant |

接下来,告诉 Rails PersonCompany 记录与许多 Lawsuit 相关联,使用 has_many' s :as 选项。

class Person < ApplicationRecord
    has_many :lawsuit_parties, as: :partiable
    has_many :lawsuits, through: :lawsuit_parties
end

将相同的 has_many :lawsuit_parties, as:partiable 添加到 Company 或以后可能出现的任何其他模型(即 JudgeJuryMember).

一旦您像这样设置了 LawsuitParty,您就应该准备就绪。

关于ruby-on-rails - ActiveRecord 与两个不同模型的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54293133/

相关文章:

ruby-on-rails - 测试 rails 中 delayed_job 插件的优先级

ruby-on-rails - 如何从 Rails 中的索引更新记录

ruby-on-rails - 如何在 RSpec 测试中打开 ActiveRecord 的 SQL 调试日志记录?

sql - 如何使用自定义加入 rails 来加载对象?

ruby-on-rails - 什么方法将从类方法返回 ActiveRecord 所有者对象?

ruby-on-rails - doorkeeper gem 在成功验证用户名和密码后给出 "invalid_client"

css - 如何在 haml 中正确显示 Font Awesome 图标

ruby-on-rails - 上帝搞乱了日期操作

ruby - 使用 Ruby SSLSockets 获取客户端地址

activerecord - 如何使用 Active Record 在 Sinatra 中静音 SQLite3 记录器?