On Jul 22, 2009, at 6:24 PM, elliottg wrote:
> Should be simple but I can't  eval a string as a method call. I have
> poked around online a fair amount to get to the bottom of this mundane
> task, with no results.
>
> I have a hash "fields" that contains a series of AR field names and an
> object called "news" which is an AR object with that has the field
> names that are coming in from the hash. I just want to assign the hash
> values to the same field value from the news object.
>
> I have tried all these variations but with no luck. Mostly just blows
> up with syntax errors.
>
>
> fields.each_pair {|key, value| news.eval("key") = value }
>
> fields.each_pair {|key, value| news."#{key}" = value }
>
> fields.each_pair {|key, value| "#{news.key}" = value }
>
> fields.each_pair {|key, value| "news.#{key} = #{value}" }
>
> fields.each_pair {|key, value| news.send key = value }
>
>
> How do I do this?
>
> Thanks, Elliott

news.update_attributes(fields)

but the syntax you were struggling to find is probably:

fields.each_pair {|key,value| news.send("#{key}=", value) }

-Rob

Rob Biedenharn          http://agileconsultingllc.com
[email protected]


--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to