So I have the models class FootballGame < ActiveRecord::Base end and class Team < ActiveRecord::Base end I want to be able to do something like this @t1 = Team.create(:name=>"Michigan Wolverines football") @t1.save @t2 = Team.create(:name=>"Penn State Nittany Lions football") @t2.save @g = FootballGame.create(:hometeam => @t1, :awayteam => @t2) @g.save my migrations: create_table :football_games do |t| t.column :hometeam, :integer t.column :awayteam, :integer end create_table :teamsdo |t| t.column :name, :integer end I''ve tried various has_one declarations but I keep ending up with NULL s for hometeam and awayteam... Anyone help me ??? Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/15/07, macarthy <justin.maccarthy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > So I have the models > > class FootballGame < ActiveRecord::Base > end > > and > > class Team < ActiveRecord::Base > end > > > I want to be able to do something like this > > @t1 = Team.create(:name=>"Michigan Wolverines football") > @t1.save > @t2 = Team.create(:name=>"Penn State Nittany Lions football") > @t2.save > > @g = FootballGame.create(:hometeam => @t1, :awayteam => @t2) > @g.save > > my migrations: > > create_table :football_games do |t| > t.column :hometeam, :integer > t.column :awayteam, :integer > end > > create_table :teamsdo |t| > t.column :name, :integer > end > > I''ve tried various has_one declarations but I keep ending up with NULL > s for hometeam and awayteam... > > Anyone help me ??? Thanks > > > > >That''s because when you have the foreign key in the FootballGames table, you need to use belongs_to instead of has_one. The easiest way is to make the columns hometeam_id and awayteam_id, and the model becomes class FootballGame < ActiveRecord::Base belongs_to :hometeam, :class_name => "Team" belongs_to :awayteam, :class_name => "Team" end If you want to keep your field names hometeam and awayteam in the db, then you need to add the :foreign_key option to belongs_to. Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I was thinking that was only solution but for some reason it seemed wrong. Guess it is the only baked in method though. FootballGame belongs to hometeam FootballGame belongs to awayteam hmmm.. Thanks Justin On Oct 16, 12:18 am, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10/15/07, macarthy <justin.maccar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > So I have the models > > > class FootballGame < ActiveRecord::Base > > end > > > and > > > class Team < ActiveRecord::Base > > end > > > I want to be able to do something like this > > > @t1 = Team.create(:name=>"Michigan Wolverines football") > > @t1.save > > @t2 = Team.create(:name=>"Penn State Nittany Lions football") > > @t2.save > > > @g = FootballGame.create(:hometeam => @t1, :awayteam => @t2) > > @g.save > > > my migrations: > > > create_table :football_games do |t| > > t.column :hometeam, :integer > > t.column :awayteam, :integer > > end > > > create_table :teamsdo |t| > > t.column :name, :integer > > end > > > I''ve tried various has_one declarations but I keep ending up with NULL > > s for hometeam and awayteam... > > > Anyone help me ??? Thanks > > That''s because when you have the foreign key in the FootballGames > table, you need to use belongs_to instead of has_one. > > The easiest way is to make the columns hometeam_id and awayteam_id, > and the model becomes > > class FootballGame < ActiveRecord::Base > belongs_to :hometeam, :class_name => "Team" > belongs_to :awayteam, :class_name => "Team" > end > > If you want to keep your field names hometeam and awayteam in the db, > then you need to add the :foreign_key option to belongs_to. > > Pat--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- Include with two references of one model of the same table
- Design question: Years/Teams/Students
- [LLVMdev] advertisement: summer 2012 internship positions available in Apple's compiler teams
- [LLVMdev] advertisement: summer 2013 internship positions now available in Apple's compiler and language runtime teams
- [LLVMdev] [cfe-dev] Summer 2015 internships in Apple's Source Tools & Program Analysis Teams