Validation problem with active record: AssociationTypeMismatch

Pat Mac
2005-04-04T23:22:43Z

[Rails] Validation problems: ActiveRecord::AssociationTypeMismatch
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