Can't modify frozen hash during validation

hi

I'm working on an app where a user inputs their physical address. When the form is submitted, we are using the USPS webservice to validate the address and update the fields as necessary.

Code looks as follows:

Address < ActiveRecord::Base   def validate     usps_address_lookup   end

  def usps_address_lookup     # call USPS service     # if error:     # errors.add_to_base usps_error.message     # else     # self.attributes = usps_address_attributes     # true   end end

the issue is that I get an exception: "can't modify frozen hash" where I update the attributes on my Address.

If I move usps_address_lookup into before_save, it works unless I have USPS validation errors which get completely ignored and the invalid address gets saved.

relevant trace:

TypeError (can't modify frozen hash):     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ attribute_methods.rb:309:in `='     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ attribute_methods.rb:309:in `write_attribute_without_dirty'     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ dirty.rb:132:in `write_attribute'     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ attribute_methods.rb:211:in `city='     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:2361:in `send'     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:2361:in `attributes='     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:2360:in `each'     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:2360:in `attributes='     /app/models/address.rb:176:in `usps_address_lookup'     /app/models/address.rb:29:in `validate'     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ validations.rb:930:in `valid_without_callbacks?'     /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ callbacks.rb:267:in `valid?'

Any ideas? Thanks!

spike grobstein wrote:

Address < ActiveRecord::Base   def validate     usps_address_lookup   end

  def usps_address_lookup     # call USPS service     # if error:     # errors.add_to_base usps_error.message     # else     # self.attributes = usps_address_attributes     # true   end end

the issue is that I get an exception: "can't modify frozen hash" where I update the attributes on my Address.

Perhaps the find of the record attached extra attributes using a join.

We actually figured it out.

We have 2 calls to our function. One to look the address up and update the fields from the USPS service and a second call which checks those results and returns validation errors.

Not the best way of doing it, but at least it works.

I am not sure if this is related to your problem, but it has the same symptom "Can't modify frozen hash", so I put it here for other people to refer if needed.

I have the error "Can't modify frozen hash" when I query an array of objects from the database, then do the following things

- Based on some conditions, I will update the array, or destroy objects in the array out of database, or add more objects to the array.

- Save the objects in the array to the database.

The error happens when the code (with some logical bugs) deletes one of the object in the database, but still keeps the object in the array. Later on, when saving the array, it saves that deleted object also, so the error "Can't modify frozen hash" happens.