diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index a13b0d01812d21171f693e66dc2abd1e42b86135..462e5e7aaf3faafc6be6bc8df810f3bc17b5297e 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -402,11 +402,7 @@ def update_attribute(name, value) verify_readonly_attribute(name) public_send("#{name}=", value) - if has_changes_to_save? - save(validate: false) - else - true - end + save(validate: false) end # Updates the attributes of the model from the passed-in hash and saves the diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 07c4a17fb127e197c3f8713dca29914eb3a78f22..0fa8ea212fd5f37451c31ee7c17562e57073efa5 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -633,6 +633,9 @@ def test_update_attribute 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 2154b50ef741ff77ff4284c4ded5de1c0d6ed249..8cd4dc352a4eb0bb74e4467c0ce9d8b4353ba527 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -65,6 +65,9 @@ def topic_id 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 @@ def before_save_for_transaction; end 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