Hey guys I''m having a little trouble what I think should be a pretty simple relationship between tables. Player.rating is a f-key to Rating.id, both fields are integers When I try to add a new player I get this error message ActiveRecord::AssociationTypeMismatch in PlayersController#create Rating(#38968656) expected, got String(#19165136) In my form I have this for the rating dropdown f.select :rating, options_for_select(Rating.all.map { |r| [r.level, r.id] }) In my models I have #player has_one :rating #rating belongs_to :player, :foreign_key => "id" The controller is pretty straight forward def create @player = Player.new(params[:player]) respond_to do |format| if @player.save flash[:notice] = "Registration Successful" format.html { redirect_to root_url } else flash[:notice] = "Error in registration" format.html { render :action => "new" } end end end Any guidance? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Also, here are the params passed to the server "player"=> { "first_name"=>"Alex", "last_name"=>"Zwinge", "email_address"=>"poolshark1172-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "phone_number"=>"512-492-5708", "rating"=>"1", <---- correct value "city"=>"1", "gender"=>"0" }, "commit"=>"Register", "controller"=>"players", "action"=>"create" -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alex Smith wrote:> Hey guys I''m having a little trouble what I think should be a pretty > simple relationship between tables. > > Player.rating is a f-key to Rating.id, both fields are integers > > When I try to add a new player I get this error message > > ActiveRecord::AssociationTypeMismatch in PlayersController#create > Rating(#38968656) expected, got String(#19165136) > > In my form I have this for the rating dropdown > f.select :rating, options_for_select(Rating.all.map { |r| [r.level, > r.id] }) > > In my models I have > > #player > has_one :rating > > #rating > belongs_to :player, :foreign_key => "id"You''ve got it backwards. foreign_key gives the name of the key on the other table, so in this case, it would be ''rating'', not ''id''. But why not follow the Rails conventions, call the column Player.rating_id , and drop the foreign key clause? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I knew it was going to be simple! Adding _id worked like a charm. Thanks for the help. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alex Smith wrote:> I knew it was going to be simple! Adding _id worked like a charm. Thanks > for the help.You''re most welcome. BTW, are you the Alex Smith I used to work with? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.