How to update Activerrecord fields dynamiclly?
I need update some fields in activerecord model. But I'll ge开发者_StackOverflowt model and names/values of fields only at runntime. Some of the fields are the other models and associations has already set up.
The best solution I could come up with is:
fields.each do |key,val|
model.send :"#{key}, val
end
Is there any other method?
Without save, attributes=:
model.attributes = fields
With save and validation, update_attributes:
model.update_attributes(fields)
fields.each do |key,val|
model.update_attribute key, val
end
or
model.update_attributes fields
精彩评论