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
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, Elliottnews.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 Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Thanks Rob. The send syntax worked perfectly! In this case I don''t think news.update_attributes(fields) will work, because fields hash values conflicts with my validations, but It helped me to think a bit differently in general. Elliott On Jul 22, 6:52 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> 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 > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org