Bug in Active Record?

I was getting this:

undefined method `to_f’ for {}:HashWithIndifferentAccess

This was happening in the after_create method. We have a table that lists fields and what validation is needed, we use this to pick out the value in the record so we can apply the validation function to the value if one has been given.

field_value = self.send(field_name) # Error was thrown here

I fixed this by calling self.reload at the beginning of the method that does the field scanning. I think that ActiveRecord doesn’t refresh its attributes hash after save and the error came from it expecting the value to be of a particular type… I don’t like calling reload, but can’t find a method that will refresh the attributes hash.

Any ideas that might help me avoid this?

The attributes should be the same whether the save fails or not.

For what it's worth, the params attribute of the controllers is a HashWithIndifferentAccess. Any chance that you were inadvertently passing params (or one of its inner hashes) rather than the ARec object?

Thanks for the input.

Discovered that this was the result of sending nils in the hash passed into the create method, as it came over the wire as XML and was then incorporated into the params array, unlike the conventional post method, which puts empty strings in there. I wrote a one-liner to remove the hash elements with nil in them and everything started working. There was also another nasty bug which was putting the HashWithIndifferentAccess thang into any empty strings and saving that into the database.

params[:person].delete_if{ |k,v| v.blank? }

Thanks again