Joshua Muheim
2006-Jul-28 18:54 UTC
[Rails] Forms: handle foreign keys (AssociationTypeMismatch)
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 -- Posted via http://www.ruby-forum.com/.
Joshua Muheim
2006-Jul-29 16:35 UTC
[Rails] Re: Forms: handle foreign keys (AssociationTypeMismatch)
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...? -- Posted via http://www.ruby-forum.com/.
Jack Christensen
2006-Jul-31 13:54 UTC
[Rails] Forms: handle foreign keys (AssociationTypeMismatch)
Joshua Muheim wrote:>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??? > >I would say use validates_presence_of :country instead of the above two lines.> 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) %> > >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.><%= 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 > > >-- Jack Christensen jackc@hylesanderson.edu