New to rails, a little model help please

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?

Also, here are the params passed to the server

“player”=>
{
“first_name”=>“Alex”,
“last_name”=>“Zwinge”,
“email_address”=>“[email protected]”,
“password”=>"[FILTERED]",
“password_confirmation”=>"[FILTERED]",
“phone_number”=>“512-492-5708”,
“rating”=>“1”, <---- correct value
“city”=>“1”,
“gender”=>“0”
},
“commit”=>“Register”,
“controller”=>“players”,
“action”=>“create”

I knew it was going to be simple! Adding _id worked like a charm. Thanks
for the help.

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
[email protected]

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
[email protected]