Ruby on rails @event.users.size为什么不起作用?
尝试获取给定事件的总用户数,我认为我所获得的应该有效,但我得到以下结果: 找不到源关联:模型团队中的团队\用户。请尝试“has_many:users,:through=>:squads,:source=>”。它是:团队、:事件、:事件\分区、:用户、:点\调整、:检查点\小组、:分区还是:检查点 我的武功很弱:-/ 事件 团队 小队 班长 使用者Ruby on rails @event.users.size为什么不起作用?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,尝试获取给定事件的总用户数,我认为我所获得的应该有效,但我得到以下结果: 找不到源关联:模型团队中的团队\用户。请尝试“has_many:users,:through=>:squads,:source=>”。它是:团队、:事件、:事件\分区、:用户、:点\调整、:检查点\小组、:分区还是:检查点 我的武功很弱:-/ 事件 团队 小队 班长 使用者 您应该删除SquadsUsers模型,只需要一个名为squads\u users的表。has_和_belish_to_许多人将自动使用此表,而不需要额
您应该删除SquadsUsers模型,只需要一个名为squads\u users的表。has_和_belish_to_许多人将自动使用此表,而不需要额外的模型。您的加入模型真的命名为SquadsUsers吗?这可能就是问题所在。应该是斯奎多瑟 另外,我认为你们想要的是项目和团队之间的多对多关系,而不是项目和团队之间的关系,对吗?在这种情况下,您需要:
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
一个团队可以派遣多个团队参加一个活动…这就是为什么我通过团队建立了活动和团队之间的关联。我已经修补了一段时间,没有取得任何进展。我对为什么我要通过小组参加比赛的解释有意义吗?我明白了!!!!问题是我的团队模型中有一个event_id列。因此,在我的事件模型中,当我说has_many:users,:through=>:squad时,我必须指定源…在本例中为:squad_users。
has_many :squads
belongs_to :event
belongs_to :team
has_and_belongs_to_many :users
belongs_to :user
belongs_to :squad
has_and_belongs_to_many :squads
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