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!