Huh--interesting, those two has_many :games calls in your Team class there.
Does that result in a single @my_team.games collection property that gives the
complete list of games, whether they were team_a or team_b? My intuition is
that the second call would sort of overwrite the first & you wouldn't see the
games from the team_a_id link.
I tried to test this design out (I'm using rails 2.0.2) and that seems to be
what happens. Only one of the two teams I added to a game had anything in its
.games collection.
I'd be tempted to do something like this instead:
class Team < ActiveRecord::Base
belongs_to :club
belongs_to :league
has_many :a_games, :class_name => 'game', :foreign_key => 'team_a_id'
has_many :b_games, :class_name => 'game', :foreign_key => 'team_b_id'
def games
# mash the two games collections together manually.
a_games + b_games
end
end
Not that any of this addresses your problem, necessarily...
-Roy
-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of
Thierry Delbart
Sent: Friday, October 24, 2008 3:37 PM
To: [email protected]
Subject: [Rails] Re: 'each' loop 2 times trough all records
Sure there should be something about it because there's 2 teams per games.
But Teams belongs to League and Games belongs to 2 Teams. But since there a
direct link between Team and League I didn't expect the Games to affect the
Team list (I would have expected it the other way around with
2 times each games).
See below my models:
class League < ActiveRecord::Base
has_many :teams
has_many :games, :through => :teams
end
class Team < ActiveRecord::Base
belongs_to :club
belongs_to :league
has_many :games, :foreign_key => 'team_a_id'
has_many :games, :foreign_key => 'team_b_id'
end
class Game < ActiveRecord::Base
belongs_to :team_a, :class_name => 'Team', :foreign_key=>'team_a_id'
belongs_to :team_b, :class_name => 'Team', :foreign_key=>'team_b_id'
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---