Rails has_one :through association

后端 未结 3 1207
终归单人心
终归单人心 2020-12-07 20:18

Rails has a has_one :through association that helps set up a one-to-one association with a third model by going through a second model. What is the real use of

3条回答
  •  囚心锁ツ
    2020-12-07 20:33

    • Inverse association: consider the classic situation user-membership-group. If a user can be a member in many groups, then a group has many members or users, and a user has many groups. But if the user can only be a member in one group, the group still has many members: class User has_one :group, :through => :membership but class Group has_many :members, :through => memberships. The intermediate model membership is useful to keep track of the inverse relationship.

    • Expandability: a has_one :through relationship can easy be expanded and extended to a has_many :through relationship

提交回复
热议问题