# Forms: handle foreign keys (AssociationTypeMismatch)

**URL:** <https://www.ruby-forum.com/t/forms-handle-foreign-keys-associationtypemismatch/100410>\
**Category:** Rails\
**Created:** [July 28, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/forms-handle-foreign-keys-associationtypemismatch/100410 "2006-07-28T20:14:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [July 28, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/forms-handle-foreign-keys-associationtypemismatch/100410/1 "2006-07-28T20:14:42Z")

</div>

Hi all

I tried and tried and tried, but I still fail in creating a form with  
proper validation and stuff for foreign keys… So I’d really like to  
get some help here.

I’m creating a booking site for DJ’s, and for every booking one can  
choose one of different countries:

class Booking \< ActiveRecord::Base  
validates\_presence\_of :country\_id  
validates\_numericality\_of :country\_id # Necessary? Correct???

belongs\_to :country,  
:dependent =\> true  
end

class Country \< ActiveRecord::Base  
has\_many :countries  
end

Now I have created a scaffold of the bookings, but sadly this does not  
include the wanted select box for country selection. So I added it  
myself:

\<%= error\_messages\_for ‘booking’ %\>  
\<%= start\_form\_tag :action =\> ‘new’ %\>

Country: \<%= collection\_select(:booking, :country, @countries, :id,  
:name, :include\_blank =\> true) %\>

\<%= end\_form\_tag %\>

So far, so good, the countries are well displayed in a select box. But  
when I submit the form, I get the following error:

ActiveRecord::AssociationTypeMismatch in BookingsController#new

Country expected, got String

RAILS\_ROOT: /Users/Josh/Webwork/psyguideorg/public/…/config/…  
Application Trace | Framework Trace | Full Trace

/Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/associations/association\_proxy.rb:134:in  
`raise_on_type_mismatch' /Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/belongs_to_association.rb:22:in`replace’  
/Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/associations.rb:850:in  
`country=' /Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1510:in`attributes=’  
/Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/base.rb:1509:in  
`attributes=' /Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1354:in`initialize\_without\_callbacks’  
/Applications/Locomotive2/Bundles/rails112.locobundle/powerpc/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/callbacks.rb:236:in  
`initialize' #{RAILS_ROOT}/app/controllers/bookings_controller.rb:27:in`new’

Can anybody tell me what to do? Do I have to add some sort of  
before\_validation method that creates the Country object instead of the  
String (which is the ID of the selected country)?

Thanks very much for help.  
Joshua

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [July 29, 2006, 10:03am UTC](https://www.ruby-forum.com/t/forms-handle-foreign-keys-associationtypemismatch/100410/2 "2006-07-29T10:03:26Z")

</div>

No one…? Please, I really need this to continue with my work, and I  
just CAN’T be the only one with this type of problem…?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [July 31, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/forms-handle-foreign-keys-associationtypemismatch/100410/3 "2006-07-31T15:55:30Z")

</div>

Joshua M. wrote:

> validates\_presence\_of :country\_id  
> validates\_numericality\_of :country\_id # Necessary? Correct???

I would say use validates\_presence\_of :country instead of the above two  
lines.

> myself:
> 
> \<%= error\_messages\_for ‘booking’ %\>  
> \<%= start\_form\_tag :action =\> ‘new’ %\>
> 
> Country: \<%= collection\_select(:booking, :country, @countries, :id,  
> :name, :include\_blank =\> true) %\>

This should be country\_id not just country. Otherwise it is assigning  
the id that comes from the form into the booking.country association  
rather than the booking.country\_id where it should be.

> Application Trace | Framework Trace | Full Trace  
> `attributes=’  
> Thanks very much for help.  
> Joshua

–  
Jack C.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 1, 2006, 11:30am UTC](https://www.ruby-forum.com/t/forms-handle-foreign-keys-associationtypemismatch/100410/4 "2006-08-01T11:30:33Z")

</div>

Thanks a lot, man! Funny, how much time sometimes is needed to find an  
error that’s caused by a few wrong characters in the code… 😉
