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