Display names

Hi,

A quick problem that is probably very easy.

I have models, controllers and views for teams and fixtures. Team model (has_many :fixtures), Fixture model (belongs_to :team). I can create a new fixture via /fixtures/new that stores the id's of the two teams selected (via collection_select's) in "home_team_id" and "away_team_id" columns in the "fixtures" table.

The trouble is I am having issues displaying the team names in the fixtures index view. Obviously normally I would use something like <%= fixture.team.name %> but the key column isn't standard naming (team_id) so that doesn't work.

Any ideas. Feel like I am being pretty dumb here!

Cheers

A quick problem that is probably very easy.

I have models, controllers and views for teams and fixtures. Team model (has_many :fixtures), Fixture model (belongs_to :team). I can create a new fixture via /fixtures/new that stores the id's of the two teams selected (via collection_select's) in "home_team_id" and "away_team_id" columns in the "fixtures" table.

The trouble is I am having issues displaying the team names in the fixtures index view. Obviously normally I would use something like <%= fixture.team.name %> but the key column isn't standard naming (team_id) so that doesn't work.

Any ideas. Feel like I am being pretty dumb here!

Check the docs for has_many... in particular...

:foreign_key Specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and “_id” suffixed. So a Person class that makes a has_many association will use “person_id“ as the default :foreign_key.

:primary_key Specify the method that returns the primary key used for the association. By default this is id.

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!

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

Thanks Colin. That solved it, although I swear I already tried to do it like that!

Your help is much appreciated.

Thanks Colin. That solved it, although I swear I already tried to do it like that!

Use a version control system (probably git) for controlling your code, and commit often. Then you can always look back and see exactly what you have tried. Do experimental stuff on a branch so you can easily chuck it away if it does not work, or merge it in if it does.

Your help is much appreciated.

Glad to be of help

Colin