Autosave callbacks shouldn't be after_save
068f092ced8483e557725542dd919ab7c516e567 registered autosave callbacks as `after_save` callbacks. This caused the regression described in #17209. Autosave callbacks should be registered as `after_update` and `after_create` callbacks, just like before. This is a partial revert of 068f092ced8483e557725542dd919ab7c516e567. Fixes #17209.
This commit is contained in:
parent
5127857947
commit
719d52db42
@ -1,3 +1,10 @@
|
||||
* Fix regression causing `after_create` callbacks to run before associated
|
||||
records are autosaved.
|
||||
|
||||
Fixes #17209.
|
||||
|
||||
*Agis Anastasopoulos*
|
||||
|
||||
* Honor overridden `rack.test` in Rack environment for the connection
|
||||
management middleware.
|
||||
|
||||
|
@ -184,7 +184,9 @@ def add_autosave_association_callbacks(reflection)
|
||||
before_save :before_save_collection_association
|
||||
|
||||
define_non_cyclic_method(save_method) { save_collection_association(reflection) }
|
||||
after_save save_method
|
||||
# Doesn't use after_save as that would save associations added in after_create/after_update twice
|
||||
after_create save_method
|
||||
after_update save_method
|
||||
elsif reflection.has_one?
|
||||
define_method(save_method) { save_has_one_association(reflection) } unless method_defined?(save_method)
|
||||
# Configures two callbacks instead of a single after_save so that
|
||||
|
@ -1,5 +1,6 @@
|
||||
require 'cases/helper'
|
||||
require 'models/bird'
|
||||
require 'models/comment'
|
||||
require 'models/company'
|
||||
require 'models/customer'
|
||||
require 'models/developer'
|
||||
@ -616,6 +617,14 @@ def test_autosave_new_record_on_has_many_can_be_disabled_per_relationship
|
||||
firm.save!
|
||||
assert !account.persisted?
|
||||
end
|
||||
|
||||
def test_autosave_new_record_with_after_create_callback
|
||||
post = PostWithAfterCreateCallback.new(title: 'Captain Murphy', body: 'is back')
|
||||
post.comments.build(body: 'foo')
|
||||
post.save!
|
||||
|
||||
assert_not_nil post.author_id
|
||||
end
|
||||
end
|
||||
|
||||
class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
|
||||
|
@ -219,6 +219,15 @@ class PostThatLoadsCommentsInAnAfterSaveHook < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
class PostWithAfterCreateCallback < ActiveRecord::Base
|
||||
self.table_name = 'posts'
|
||||
has_many :comments, foreign_key: :post_id
|
||||
|
||||
after_create do |post|
|
||||
update_attribute(:author_id, comments.first.id)
|
||||
end
|
||||
end
|
||||
|
||||
class PostWithCommentWithDefaultScopeReferencesAssociation < ActiveRecord::Base
|
||||
self.table_name = 'posts'
|
||||
has_many :comment_with_default_scope_references_associations, foreign_key: :post_id
|
||||
|
Loading…
Reference in New Issue
Block a user