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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Jan 31, 2008 3:56 AM, Francis Fish <francis.fish-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 saveThe attributes should be the same whether the save fails or not. -- Greg Donald http://destiney.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? On Jan 31, 4:56 am, "Francis Fish" <francis.f...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
On Feb 1, 8:43 pm, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org>
wrote:> 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?
>
> On Jan 31, 4:56 am, "Francis Fish"
<francis.f...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > 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
> > therecordso 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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---