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'

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 %>
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.

Reply via email to