aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-12-25 15:18:10 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-12-25 15:21:30 +0900
commit732aa34b6e6459ad66a3d3ad107cfff75cc45160 (patch)
tree6a3382653bf736cc71d23410c8a1b0b337b8e7f7 /activerecord
parent91a4a820feeb878dd1b388befa56dd469933d17c (diff)
parentc53287b2370af2b011cdc4c583a50fbcd6fd88ed (diff)
downloadrails-732aa34b6e6459ad66a3d3ad107cfff75cc45160.tar.gz
rails-732aa34b6e6459ad66a3d3ad107cfff75cc45160.tar.bz2
rails-732aa34b6e6459ad66a3d3ad107cfff75cc45160.zip
Merge pull request #27780 from mikelikesbikes/fix-update-attribute-callbacks-issue
save attributes changed by callbacks after update_attribute
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/persistence.rb6
-rw-r--r--activerecord/test/cases/persistence_test.rb3
-rw-r--r--activerecord/test/models/topic.rb7
4 files changed, 15 insertions, 5 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 791de1f2d0..3f1908ec18 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Fix to invoke callbacks when using `update_attribute`.
+
+ *Mike Busch*
+
* Fix `count(:all)` to correctly work `distinct` with custom SELECT list.
*Ryuta Kamizono*
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index a13b0d0181..462e5e7aaf 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -402,11 +402,7 @@ module ActiveRecord
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 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