Reputation: 555
Why is my program trying to find teamplayers.player_id(which does not exists) when trying to run this line: <%= select_tag "test3", options_from_collection_for_select(Teamplayer.joins(:live_player), "teamid", "playerid") %>
I want to join on the fields playerid(from teamplayers) and Nid(from Players). I tried creating a foriegn key but that did not work
UPDATE Here is my teamplayers migrate file, there is no name a attribute, the related in name for the playerid that is stored in this table is in the players table.
class CreateTeamplayers < ActiveRecord::Migration
def change
create_table :teamplayers do |t|
t.string :playerid
t.string :teamid
t.timestamps
end
end
end
Upvotes: 0
Views: 197
Reputation: 10898
The options_from_collection_for_select
method signature is as follows:
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
So you're passing in the collection Teamplayer.joins(:live_player)
, the value method teamid
and the text method playerid
.
This means that when it's trying to build your select options, it will call teamplayer.teamid
and teamplayer.playerid
on each member of the collection to create each option.
Assuming your Teamplayer
instances have a name
attribute, just try the following to see how this works:
options_from_collection_for_select(Teamplayer.joins(:live_player), 'id', 'name')
Note that without the 4th argument, rails will not know which option should be selected in a form where you already have data selected. For example when re-rendering the form after failing to create/update a record due to validation errors.
Upvotes: 1