2012-08-09 96 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