开发者

undefined method `update_attribute' for ActiveRecord::Relation when called on record retrieved through inheritance

I have a polymorphic association relationship happening, and when I retrieve a record through it, such as:

hour = my_model.find( keeper_id ).hours.where( "day = ?", day_index )

Then try to call update_attributes on it:

hour.update_attribute(:start_time, start_ti开发者_JAVA技巧me)

I get an error:

NoMethodError (undefined method `update_attribute' for #<ActiveRecord::Relation:0x0000010458c708>):
  activerecord (3.0.0) lib/active_record/relation.rb:373:in `method_missing'

I assume it's because I retrieved the record through the relationship. How can I get around this?


Update attribute works on a single record, not on a collection. Use update_all instead (syntax is a little bit different).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜