2011-11-18 73 views
0

目前,我有以下型號:爲什麼在使用has_many時不能使用rails 3找到我的關聯?

class Player < ActiveRecord::Base 
    belongs_to :team 
    belongs_to :user 
end 

class Team < ActiveRecord::Base 
    has_many :users, :through => :players 

end 

class User < ActiveRecord::Base 
    has_many :teams, :through => :players 
end 

當我做前端以下

<%= @team.users %> 

我得到異常:

找不到關聯:玩家模型團隊

,當我嘗試:

<%= @team.players %> 

我得到如下:

未定義的方法 '玩家' 的#

任何線索可能會發生?

回答

2

我相信你還需要包括你的加盟模式,像這樣

class Player < ActiveRecord::Base 
    belongs_to :team 
    belongs_to :user 
end 

class Team < ActiveRecord::Base 
    has_many :players 
    has_many :users, :through => :players 

end 

class User < ActiveRecord::Base 
    has_many :players 
    has_many :teams, :through => :players 
end 
相關問題