undefined method on ActiveRecord::Relation object

自古美人都是妖i 提交于 2019-12-24 07:25:47

问题


the following code

fieldvalue = Admin::FieldValue.where(:item_id => @admin_item.id, :field_id => key)
fieldvalue.update_attributes(:value => value)

raise the following error

NoMethodError (undefined method `update_attributes' for #<ActiveRecord::Relation:0x00000102dfc868>):

why is it a Relation object and not a FieldValue object, what's the right way to manage this


回答1:


fieldvalue should return only one value?

fieldvalue = Admin::FieldValue.where(:item_id => @admin_item.id, :field_id => key).first
fieldvalue.update_attributes(:value => value)

Or if you expect multiple items:

fieldvalue = Admin::FieldValue.where(:item_id => @admin_item.id, :field_id => key)
fieldvalue.update_all(:value => value)


来源:https://stackoverflow.com/questions/11107775/undefined-method-on-activerecordrelation-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!