【问题标题】:join same rails models twice, eg people has_many clubs through membership AND people has_many clubs through committee两次加入相同的 Rails 模型,例如,人们通过会员拥有_many 俱乐部和人们通过委员会拥有_many 俱乐部
【发布时间】:2010-06-04 08:36:40
【问题描述】:

型号: * 人 * 俱乐部

关系 * 会员资格 * 委员会

人们应该能够加入俱乐部(会员) 人们应该能够成为俱乐部(委员会)的董事会成员

对于我的应用程序,这些涉及到非常不同的功能,所以我不希望使用标志来设置 (is_board_member) 或类似的。

我发现自己想写:

人 has_many :clubs :through => :membership # :as => :member? :foreign_key => :member_id? has_many :clubs :through => :committee # as (above)

但我不确定如何将它们拼接在一起

【问题讨论】:

    标签: ruby-on-rails activerecord polymorphism has-and-belongs-to-many polymorphic-associations


    【解决方案1】:

    试试

    has_many :committee_clubs, :through => :committee, :source => :clubs
    has_many :membership_clubs, :through => :membership, :source => :clubs
    

    【讨论】:

    • 您将不得不更改 People 模型以包括 commitee_clubs_id 和 members_clubs_id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 2019-04-19
    相关资源
    最近更新 更多