Let's pretend I want to set all the fields I specify to be = "frog"
In the model I can set each one manually using:
self.field1 = 'frog'
self.desc_field = 'frog'
self.fieldx = 'frog'
etc....
But how can I this by putting the field names in an array?
When I try
fields_array=['field1','desc_field','fieldx']
fields_array.each { |field|
self.field = 'frog'
}
It does not work. Any suggestions?
Try using the
send
method:John Topley's answer above is basically correct, however since you want to assign values you want to doing something like:
Note the added equal sign. With that you're doing
self.field1='frog'
rather thanself.field1('frog')
.