# New to rails, a little model help please

**URL:** <https://www.ruby-forum.com/t/new-to-rails-a-little-model-help-please/189679>\
**Category:** Rails\
**Created:** [June 5, 2010, 12:14am UTC](https://www.ruby-forum.com/t/new-to-rails-a-little-model-help-please/189679 "2010-06-05T00:14:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [June 5, 2010, 12:14am UTC](https://www.ruby-forum.com/t/new-to-rails-a-little-model-help-please/189679/1 "2010-06-05T00:14:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [June 5, 2010, 12:21am UTC](https://www.ruby-forum.com/t/new-to-rails-a-little-model-help-please/189679/2 "2010-06-05T00:21:08Z")

</div>

Also, here are the params passed to the server

“player”=\>  
{  
“first\_name”=\>“Alex”,  
“last\_name”=\>“Zwinge”,  
“email\_address”=\>“removed\_email\_address@domain.invalid”,  
“password”=\>"[FILTERED]",  
“password\_confirmation”=\>"[FILTERED]",  
“phone\_number”=\>“512-492-5708”,  
“rating”=\>“1”, \<---- correct value  
“city”=\>“1”,  
“gender”=\>“0”  
},  
“commit”=\>“Register”,  
“controller”=\>“players”,  
“action”=\>“create”

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [June 5, 2010, 12:36am UTC](https://www.ruby-forum.com/t/new-to-rails-a-little-model-help-please/189679/3 "2010-06-05T00:36:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [June 5, 2010, 12:48am UTC](https://www.ruby-forum.com/t/new-to-rails-a-little-model-help-please/189679/4 "2010-06-05T00:48:45Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [June 5, 2010, 12:30am UTC](https://www.ruby-forum.com/t/new-to-rails-a-little-model-help-please/189679/5 "2010-06-05T00:30:16Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
