Pat Mac
2005-Apr-05 11:40 UTC
Validation problem with active record: Association Type Mismatch
Hi everyone, I''m starting on my first rails project and I have a problem that I''m sure has a simple solution. I have two models, an address model and a state model. The address belongs_to :state, foreign_key=>"state" and validate_associated :state I have new & edit controllers for address which contain a drop down list for the state choice. My list was created with options_from_collection_for_select @states, "id", "name", @address.state When I go to the new or edit page, everything looks good. However, when I submit my new record (or edit an existing one) I get an ActiveRecord AssociationTypeMismatch Expected type State but found string instead. I can see that the proper ID is being returned, it''s just being read by ruby as a string and I''m not sure how to cast it. I tried using the various before_validation functions, but they never seem to be called (I was explicitly throwing exceptions inside them to make sure). I''m totally stuck. --Pat
Sebastian Kanthak
2005-Apr-05 16:17 UTC
Re: Validation problem with active record: Association Type Mismatch
Pat Mac wrote:>I have new & edit controllers for address which contain a drop down >list for the state choice. My list was created with >options_from_collection_for_select @states, "id", "name", >@address.state > >When I go to the new or edit page, everything looks good. However, >when I submit my new record (or edit an existing one) I get an >ActiveRecord AssociationTypeMismatch Expected type State but found >string instead. I can see that the proper ID is being returned, it''s > >what''s the name of your select field? If you name it like your "state" attribute, rails will try to assign the "id" string to this attribute which fails, because it expects a state object. If you name it after the column name (e.g. "state_id") it should work. Sebastian
Pat Mac
2005-Apr-05 20:10 UTC
Re: Validation problem with active record: Association Type Mismatch
On Apr 5, 2005 12:17 PM, Sebastian Kanthak <sebastian.kanthak-ZS8b95Whz3sUSW6y5lq3GQ@public.gmane.org> wrote:> Pat Mac wrote: > > >I have new & edit controllers for address which contain a drop down > >list for the state choice. My list was created with > >options_from_collection_for_select @states, "id", "name", > >@address.state > > > >When I go to the new or edit page, everything looks good. However, > >when I submit my new record (or edit an existing one) I get an > >ActiveRecord AssociationTypeMismatch Expected type State but found > >string instead. I can see that the proper ID is being returned, it''s > > > > > what''s the name of your select field? If you name it like your "state" > attribute, rails will try to assign the "id" string to this attribute > which fails, because it expects a state object. If you name it after the > column name (e.g. "state_id") it should work. > > Sebastian > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Interesting... shouldn''t the :foreign_key=>"state" allow this to work? Changing everything from state to state_id worked (including the database), but I would hope I don''t need to rename all of my foreign key columns just for rails. BTW.. sorry for the multiple posts. The "postmaster" spam was making me think my messages were being bounced.
Jarkko Laine
2005-Apr-06 06:05 UTC
Re: Validation problem with active record: Association Type Mismatch
On 5.4.2005, at 23:10, Pat Mac wrote:>> > Interesting... shouldn''t the :foreign_key=>"state" allow this to work? > Changing everything from state to state_id worked (including the > database), but I would hope I don''t need to rename all of my foreign > key columns just for rails.The problem here is that when you declare that address belongs_to :state, @address.state will be automatically used as a method to get to that specific State object. Therefore Rails can''t know that you''re in fact referring to a column named "state" instead of the object. You can work around this by using something else as your association name: class Address ... belongs_to :home_state, :class_name => "state", :foreign_key => "state" end Now you can use @address.home_state which refers to the object and @address.state which refers to the foreign key field. That way you shouldn''t have to change your existing databases. HTH, Jarkko> > BTW.. sorry for the multiple posts. The "postmaster" spam was making > me think my messages were being bounced. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails