Example: def somemethod(variable) value = Record.find(:all) value.each do |row| row.colone = "foo" row.coltwo = "bar" row.__send__(variable) = "keke" row.save! end end somemethod(colthree) => unexpected ''='', expecting kEND I can''t do row.variable, that will create an undefined method. What can I do to place in the expected column? -- Posted via http://www.ruby-forum.com/.
I figured it out myself. def somemethod(variable) value = Record.find(:all) value.each do |row| row.colone = "foo" row.coltwo = "bar" row[variable.to_sym] = "keke" row.save! end end Making the variable into a symbol works. -- Posted via http://www.ruby-forum.com/.
Rob Biedenharn
2009-Aug-20 19:21 UTC
Re: Display variable as method in an activerecord call?
On Aug 20, 2009, at 3:08 PM, Alpha Blue wrote:> Example: > > def somemethod(variable) > value = Record.find(:all) > value.each do |row| > row.colone = "foo" > row.coltwo = "bar" > row.__send__(variable) = "keke" > row.save! > end > end > > somemethod(colthree) > > => unexpected ''='', expecting kEND > > I can''t do row.variable, that will create an undefined method. What > can > I do to place in the expected column? > --Do you want something like: row.__send__("#{variable}=", "keke") If your meaning is to assign to a column whose name is supplied, then you might also do: row[variable] = ''keke'' or even row.write_attribute(variable, ''keke'') -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org