views:

24

answers:

2

Trying to get to total number of users for a given event and I'm thinking what I've got should work, but I get the following:

Could not find the source association(s) :squads_users in model Squad. Try 'has_many :users, :through => :squads, :source => '. Is it one of :team, :event, :event_division, :users, :point_adjustments, :checkpoint_squads, :division, or :checkpoints?

My ActiveRecord Kung Fu is weak :-/

Event

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

Team

 has_many :squads

Squad

 belongs_to :event
 belongs_to :team
 has_and_belongs_to_many :users

SquadsUsers

 belongs_to :user
 belongs_to :squad

User

 has_and_belongs_to_many :squads
A: 

You should remove your SquadsUsers model and just have a table called squads_users. has_and_belongs_to_many will automatically use this table without needing the additional model.

Beerlington
A: 

Is your join model really named SquadsUsers? That might be the problem. It should be SquadUser.

Also, I think you want a many-to-many relationship between Event and Team, not Event and Squad, correct? In which case you need this:

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
zetetic
A Team can send more than one Squad to an Event...which is why I set up the association between Events and Teams through Squads.
Danger Angell
I've been tinkering with this for a while and not making any progress. Does my explanation for why I'm joining Teams to Events through Squads make sense?
Danger Angell
I got it!!!! The problem was that I have an event_id column in my squad model. So in my event model when I said has_many :users, :through => :squad I had to specify the source...in this case :squad_users.
Danger Angell