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.
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.
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
Thanks to everyone, your information has been most usefull
Thanks man, maybe I'll take your word for help? Thanks a lot