# Bug in Active Record?

**URL:** <https://www.ruby-forum.com/t/bug-in-active-record/127677>\
**Category:** Rails\
**Created:** [January 31, 2008, 10:56am UTC](https://www.ruby-forum.com/t/bug-in-active-record/127677 "2008-01-31T10:56:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![codepanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@codepanda](https://www.ruby-forum.com/u/codepanda)\
**Post date:** [January 31, 2008, 10:56am UTC](https://www.ruby-forum.com/t/bug-in-active-record/127677/1 "2008-01-31T10:56:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![codepanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@codepanda](https://www.ruby-forum.com/u/codepanda)\
**Post date:** [February 1, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/bug-in-active-record/127677/2 "2008-02-01T21:35:49Z")

</div>

On Jan 31, 2008 3:56 AM, Francis F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

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

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![codepanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@codepanda](https://www.ruby-forum.com/u/codepanda)\
**Post date:** [February 1, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/bug-in-active-record/127677/3 "2008-02-01T21:44:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![codepanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@codepanda](https://www.ruby-forum.com/u/codepanda)\
**Post date:** [February 5, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/bug-in-active-record/127677/4 "2008-02-05T17:15:31Z")

</div>

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
