2022-06-15

Can you use "belongs_to' if a model can be associated to multiple owners/parents?

I have a many-to-many relationship between Policy(s) and Rule(s). A policy can have multiple rules. A given rule can belong to multiple policies. There's a mapping table PolicyRule(s) that exists (plus other metadata about the relation). From what I understand doing has_and_belongs_to_many would not work if the mapping table has other columns. So I'm looking into doing a has_many :through. This doc has an example of Document -> Section -> Paragraphs that I'm following but I'm not sure if it's correct for me to say that rule belongs_to: policy_rule when it can belong to more than one.

class Policy < Base
    has_many :rule, through: :policy_rule
end

class PolicyRule < Base
    belongs_to :policy
    has_many :rules
end

class Rule < Base
    belongs_to: :policy_rule
end



No comments:

Post a Comment