为什么@ event.users.size不起作用?

时间:2010-09-30 18:11:18

标签: ruby-on-rails activerecord

尝试获取给定事件的用户总数,我正在考虑我应该做的事情,但我得到以下内容:

无法在模型Squad中找到源关联:squads_​​users。试试'has_many:users,:through => :小队,:source => ”。它是以下之一:team,:event,:event_division,:users,:point_adjustments,:checkpoint_squads,:division,或:checkpoints?

我的ActiveRecord Kung Fu很弱: - /

事件

has_many :squads
has_many :users, :through => :squads

团队

 has_many :squads

 belongs_to :event
 belongs_to :team
 has_and_belongs_to_many :users

SquadsUsers

 belongs_to :user
 belongs_to :squad

用户

 has_and_belongs_to_many :squads

2 个答案:

答案 0 :(得分:0)

你应该删除你的SquadsUsers模型,并且只有一个名为squads_​​users的表。 has_and_belongs_to_many将自动使用此表而无需其他模型。

答案 1 :(得分:0)

你的连接模型真的命名为SquadsUsers吗?这可能是问题所在。它应该是SquadUser。

另外,我认为你想要Event和Team之间的多对多关系,而不是Event和Squad,对吗?在这种情况下,你需要这个:

Event
has_many :event_teams
has_many :teams, :through => :event_teams

EventTeam
belongs_to :event
belongs_to :team

Team
has_many :squads

Squad
belongs_to :team
has_many :squad_users
has_many :users, :through => :squad_users

SquadUser
belongs_to :squad
belongs_to :user

User
has_many :squads