提交 c53287b2 编写于 作者: M Mike Busch

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.
上级 09017552
......@@ -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
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册