On 24 October 2011 23:03, Graham Dawe <[email protected]> wrote: > Philip Hallstrom wrote in post #1028174: > >> :foreign_key >> Specify the foreign key used for the association. >> :primary_key >> Specify the method that returns the primary key used for the >> association. By default this is id. > > Hi Philip, > > Thanks for your reply. I have tried to use the :foreign_key with the > following results. (fyi fixtures has changed to matches). The matches > table has "home_team_id" and "away_team_id" columns. > > match.rb > > belongs_to :home_team, :foreign_key => 'home_team_id' > belongs_to :away_team, :foreign_key => 'away_team_id'
You need to use :class_name here so that it knows what the foreign keys are pointing to. > > team.rb > > has_many :home_matches, :class_name => 'Team' > has_many :away_matches, :class_name => 'Team' > > matches_controller.rb > > @matches = Match.all > @teams = Team.all > > matches.html.erb > > <% @matches.each do |match| %> > Home <%= match.home_team %> You probably want match.home_team.name or something like that, you are trying to display the whole team object. Colin > Away <%= match.away_team %><br> > <% end %> > > I get the following when I load matches/index.html.erb ... > > Home #<Match:0x103cfafe0> Away #<Match:0x103cee948> > Home #<Match:0x103cebf18> Away #<Match:0x103ce8278> > > The dev/log reads the following > > Started GET "/matches" for 127.0.0.1 at Mon Oct 24 23:00:54 +0100 2011 > Processing by MatchesController#index as HTML > Match Load (0.5ms) SELECT "matches".* FROM "matches" > Team Load (0.4ms) SELECT "teams".* FROM "teams" > Match Load (0.3ms) SELECT "matches".* FROM "matches" WHERE > "matches"."id" = 1 LIMIT 1 > Match Load (0.2ms) SELECT "matches".* FROM "matches" WHERE > "matches"."id" = 2 LIMIT 1 > CACHE (0.0ms) SELECT "matches".* FROM "matches" WHERE "matches"."id" > = 2 LIMIT 1 > Match Load (0.2ms) SELECT "matches".* FROM "matches" WHERE > "matches"."id" = 3 LIMIT 1 > Match Load (0.2ms) SELECT "matches".* FROM "matches" WHERE > "matches"."id" = 7 LIMIT 1 > Match Load (0.1ms) SELECT "matches".* FROM "matches" WHERE > "matches"."id" = 6 LIMIT 1 > Rendered matches/index.html.erb within layouts/application (16.0ms) > Completed 200 OK in 38ms (Views: 20.5ms | ActiveRecord: 1.8ms) > > > So, it is reading the table but just returning the wrong stuff. I don't > exactly know what one of <Match:0x103cebf18> is called so I don't know > what to search for to correct it. > > Any help much appreciated! > > -- > 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. > > -- gplus.to/clanlaw -- 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.

