I’ve run into a bizarre. I have a model GuardianUpdate with a field “read” which is a boolean. When I go to call update_attribute(:read, true) on a record, rails fails to actually update the record.
Here is my console output:
1.9.3p0 :026 > g = GuardianUpdate.find(1)
GuardianUpdate Load (0.7ms) SELECT `guardian_updates`.* FROM `guardian_updates` WHERE `guardian_updates`.`id` = 1 LIMIT 1
=> #<GuardianUpdate id: 1, update_id: 2, guardian_id: 11, read: true, created_at: "2012-04-21 04:06:45", updated_at: "2012-04-21 04:06:45">
1.9.3p0 :027 > g.update_attribute(:read, false)
(0.3ms) BEGIN
Update Load (0.4ms) SELECT `updates`.* FROM `updates` WHERE `updates`.`id` = 2 LIMIT 1
(0.1ms) COMMIT
=> true
1.9.3p0 :028 > g.reload
GuardianUpdate Load (0.5ms) SELECT `guardian_updates`.* FROM `guardian_updates` WHERE `guardian_updates`.`id` = 1 LIMIT 1
=> #<GuardianUpdate id: 1, update_id: 2, guardian_id: 11, read: true, created_at: "2012-04-21 04:06:45", updated_at: "2012-04-21 04:06:45">
1.9.3p0 :029 >
As you can tell, there is no UPDATE sql statement anywhere. Here’s my GuaridanUpdate model:
class GuardianUpdate < ActiveRecord::Base
belongs_to :update
belongs_to :guardian
end
Any thoughts?
I should mention that update_column(:read, true) works, no problem at all.
The
belongs_to :updateis creating anupdateaccessor that is overwriting the internal rails method of the same name. That method is the one responsible for actually saving changes to an existant object.Rename the association and you should be ok. Rails is suppose to raise a
DangerousAttributeNameexception to warn you of this – not sure why this isn’t happening