2012-08-09 101 views
0

我很难找出最好的方法来做到这一点。我有一个User模型和一个锦标赛模型,我建立了一个has_many:这两个模型之间的关系叫'followed_tournaments',这样用户就可以参加比赛了。因此,我已经在锦标赛模型中拥有了has_many:用户模型中的锦标赛和has_many:用户,这样锦标赛有很多追随者,用户可以关注很多锦标赛。对同一型号的多种关系

我想建立另一个habtm或has_many:通过关系,以便用户可以被视为锦标赛的“贡献者” - 与我已经建立的完全独立的关系。我希望锦标赛能让任何数量的贡献者和用户参与许多比赛。

什么是最好的方式去实现呢?

+0

,也许你可以把那对FollowedTournament模型。也许一些名为贡献的布尔attr。 – 2012-08-09 05:10:08

回答

0

使用sourceclass_name

class Tournament < ActiveRecord::Base 
    has_many :users # ... whatever 

    has_many :contributions 

    # using class_name 
    has_many :contributors, :through => :contributions 

    # using source 
    has_many :contributors, :through => :contributions, :source => :user 
end 

class Contribution < ActiveRecord::Base 
    belongs_to :tournament 

    # using class_name 
    belongs_to :contributor, :class_name => 'User' 

    # using source 
    belongs_to :user 
end