I have the following tables teams id :string name :string and matches id: home_team :team_id visitor_team :team_id how can I reflect that kind of relationship in a RoR model? thanks and keep up the good work.
jask wrote:> I have the following tables > teams > id :string > name :string > > and > > matches > id: > home_team :team_id > visitor_team :team_id > > how can I reflect that kind of relationship in a RoR model? > thanks and keep up the good work.teams id :integer name :string matches id :integer team_id :integer visitor_team :integer date_scheduled :datetime class Team < ActiveRecord::Base has_many :matches has_many :visitor_teams, :through => :matches end class Match < ActiveRecord::Base belongs_to :team belongs_to :visitor_team, :class_name => "Team" end The home team goes in your team_id column which is associated with team.id. You create a self-referential association to the visitor_team. Then you call them using: @matches = Match.find(:all, :joins => [:team, :visitor_team], :order => :date_scheduled) Retrieve the data: @matches.each do |match| match.team.name match.visitor_team.name match.date_scheduled end You really should learn about associations before you go through all this trouble. You can check out http://guides.rubyonrails.org/ . It just so happens that I run a football site so I do pretty much exactly what you are after. If you have some questions, I''ll be happy to help. -- Posted via http://www.ruby-forum.com/.
On Jul 29, 2:05 am, jask <sahag...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have the following tables > teams > id :string > name :string > > and > > matches > id: > home_team :team_id > visitor_team :team_id > > how can I reflect that kind of relationship in a RoR model? > thanks and keep up the good work.See http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table Fred
Hi! Try this: class Team < AR::B has_many :home_matches, :class_name => ''Match'', :foreign_key => :home_team has_many :visit_matches, :class_name => ''Match'', :foreign_key => :visit_team end class Match < AR::B belongs_to :home_team, :class_name => ''Team'' belongs_to :visit_team, :class_name => ''Team'' end On 29 июл, 10:05, jask <sahag...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have the following tables > teams > id :string > name :string > > and > > matches > id: > home_team :team_id > visitor_team :team_id > > how can I reflect that kind of relationship in a RoR model? > thanks and keep up the good work.
Thanks to everyone, your information has been most usefull On 29 jul, 07:00, Rakoth <rakot...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! Try this: > > class Team < AR::B > has_many :home_matches, :class_name => ''Match'', :foreign_key > => :home_team > has_many :visit_matches, :class_name => ''Match'', :foreign_key > => :visit_team > end > > class Match < AR::B > belongs_to :home_team, :class_name => ''Team'' > belongs_to :visit_team, :class_name => ''Team'' > end > > On 29 июл, 10:05,jask<sahag...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I have the following tables > > teams > > id :string > > name :string > > > and > > > matches > > id: > > home_team :team_id > > visitor_team :team_id > > > how can I reflect that kind of relationship in a RoR model? > > thanks and keep up the good work.
Thanks man, maybe I''ll take your word for help? Thanks a lot On 29 jul, 06:58, Alpha Blue <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> jaskwrote: > > I have the following tables > > teams > > id :string > > name :string > > > and > > > matches > > id: > > home_team :team_id > > visitor_team :team_id > > > how can I reflect that kind of relationship in a RoR model? > > thanks and keep up the good work. > > teams > id :integer > name :string > > matches > id :integer > team_id :integer > visitor_team :integer > date_scheduled :datetime > > class Team < ActiveRecord::Base > has_many :matches > has_many :visitor_teams, :through => :matches > end > > class Match < ActiveRecord::Base > belongs_to :team > belongs_to :visitor_team, :class_name => "Team" > end > > The home team goes in your team_id column which is associated with > team.id. You create a self-referential association to the visitor_team. > > Then you call them using: > > @matches = Match.find(:all, :joins => [:team, :visitor_team], :order => > :date_scheduled) > > Retrieve the data: > > @matches.each do |match| > match.team.name > match.visitor_team.name > match.date_scheduled > end > > You really should learn about associations before you go through all > this trouble. You can check outhttp://guides.rubyonrails.org/. > > It just so happens that I run a football site so I do pretty much > exactly what you are after. If you have some questions, I''ll be happy > to help. > > -- > Posted viahttp://www.ruby-forum.com/.