问题
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