From c53287b2370af2b011cdc4c583a50fbcd6fd88ed Mon Sep 17 00:00:00 2001 From: Mike Busch Date: Mon, 23 Jan 2017 12:09:14 -0500 Subject: save attributes changed by callbacks after update_attribute update_attribute previously stopped execution, before saving and before running callbacks, if the record's attributes hadn't changed. [The documentation](http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute) says that "Callbacks are invoked", which was not happening if the persisted attributes hadn't changed. --- activerecord/test/cases/persistence_test.rb | 3 +++ activerecord/test/models/topic.rb | 7 +++++++ 2 files changed, 10 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 07c4a17fb1..0fa8ea212f 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -633,6 +633,9 @@ class PersistenceTest < ActiveRecord::TestCase Topic.find(1).update_attribute(:approved, false) assert !Topic.find(1).approved? + + Topic.find(1).update_attribute(:change_approved_before_save, true) + assert Topic.find(1).approved? end def test_update_attribute_for_readonly_attribute diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 2154b50ef7..8cd4dc352a 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -65,6 +65,9 @@ class Topic < ActiveRecord::Base after_initialize :set_email_address + attr_accessor :change_approved_before_save + before_save :change_approved_callback + class_attribute :after_initialize_called after_initialize do self.class.after_initialize_called = true @@ -96,6 +99,10 @@ class Topic < ActiveRecord::Base def before_destroy_for_transaction; end def after_save_for_transaction; end def after_create_for_transaction; end + + def change_approved_callback + self.approved = change_approved_before_save unless change_approved_before_save.nil? + end end class ImportantTopic < Topic -- cgit v1.2.3