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.

On Aug 20, 2009, at 3:08 PM, Alpha B. wrote:

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 B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs