开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜