Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_time)

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?

like image 349
99miles Avatar asked Oct 11 '10 22:10

99miles


1 Answers

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

like image 163
Simone Carletti Avatar answered Nov 15 '22 05:11

Simone Carletti