Display variable as method in an activerecord call?

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?

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.

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@AgileConsultingLLC.com