Reputation: 1114
I have nested results upon matches like this
/matches/16/results/13/edit
Where I have the following select field, this shows the correct information ( team.name and its team.id)
<%= f.collection_select :winner, @select_winner_loser, :id, :name %>
Now when I try to edit my result and selecting a winner I get this:
ActiveRecord::AssociationTypeMismatch Team(#10504340) expected, got String(#6163240)
"Raised when an object assigned to an association has an incorrect type." http://api.rubyonrails.org/classes/ActiveRecord/AssociationTypeMismatch.html
Winner is a team object, result.rb looks like this
class Result < ActiveRecord::Base
has_one :match
belongs_to :winner, class_name: "Team"
belongs_to :loser, class_name: "Team"
end
@select_winner_loser comes from my results_controller
def edit
@select_winner_loser = []
@select_winner_loser << @match.top
@select_winner_loser << @match.bottom
end
Match.top & bottom is also team objects
class Match < ActiveRecord::Base
belongs_to :top, class_name: "Team"
belongs_to :bottom, class_name: "Team"
...
belongs_to :result
end
I dont see why I would get this since I have the correct objects in my select field, any ideas?
Thanks
Upvotes: 4
Views: 15763
Reputation: 5269
Change
<%= f.collection_select :winner, @select_winner_loser, :id, :name %>
to
<%= f.collection_select :winner_id, @select_winner_loser, :id, :name %>
and your permitted parameters accordingly. Rails will create a Team
object when it sees the _id
in the name.
Upvotes: 16