andrew.mansfield-jRYH5KdBSDQ@public.gmane.org
2008-Feb-26 20:13 UTC
Association problem in one direction
I have these models class Game < ActiveRecord::Base belongs_to :home_team, :class_name => ''Team'' belongs_to :away_team, :class_name => ''Team'' belongs_to :field end class Person < ActiveRecord::Base has_many :affiliations has_many :teams, :through => :affiliations end class Team < ActiveRecord::Base has_many :affiliations has_many :players, :through => :affiliations, :source => :person has_many :games end class Affiliation < ActiveRecord::Base belongs_to :person belongs_to :team end I with the associations above, I can get the players for the teams, the home team and away team players from the games. I want to be able to go back and get the games that each team and/or player plays in. The associations work in one direction but not in the other. How do I work back through the home_team and away_team connections and get home games, away games and the totals for a given player or team? Any ideas? Thanks Andrew --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rails cannot infer how to get from Games --> Teams, from your models rails is looking for team_id in Games Based on your models, I can see only one way: class Team < ActiveRecord::Base has_many :affiliations has_many :players, :through => :affiliations, :source => :person has_many :home_games, :class => ''Games'', :foreign_key => ''home_team_id'' has_many :away_games, :class => ''Games'', :foreign_key => ''away_team_id'' end You could then access team.away_games and team.home_games On Feb 26, 3:13 pm, "andrew.mansfi...-jRYH5KdBSDQ@public.gmane.org" <crashbrot...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have these models > > class Game < ActiveRecord::Base > belongs_to :home_team, :class_name => ''Team'' > belongs_to :away_team, :class_name => ''Team'' > belongs_to :field > end > class Person < ActiveRecord::Base > has_many :affiliations > has_many :teams, :through => :affiliations > end > class Team < ActiveRecord::Base > has_many :affiliations > has_many :players, :through => :affiliations, :source => :person > has_many :games > end > class Affiliation < ActiveRecord::Base > belongs_to :person > belongs_to :team > end > > I with the associations above, I can get the players for the teams, > the home team and away team players from the games. > > I want to be able to go back and get the games that each team and/or > player plays in. The associations work in one direction but not in the > other. How do I work back through the home_team and away_team > connections and get home games, away games and the totals for a given > player or team? > > Any ideas? > > Thanks > > Andrew--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---