Allow subclasses to redefine autosave callbacks for associated records

This commit is contained in:
Andrey Subbota 2018-07-17 12:55:30 +03:00
parent f0c917c7d4
commit 35ee756a36
4 changed files with 32 additions and 1 deletions

@ -1,3 +1,9 @@
* Allow subclasses to redefine autosave callbacks for associated records.
Fixes #33305.
*Andrey Subbota*
* Don't impose primary key order if limit() has already been supplied.
Fixes #23607

@ -149,7 +149,7 @@ module ClassMethods # :nodoc:
private
def define_non_cyclic_method(name, &block)
return if method_defined?(name)
return if instance_methods(false).include?(name)
define_method(name) do |*args|
result = true; @_already_called ||= {}
# Loop prevention for validation of associations

@ -14,6 +14,7 @@
require "models/order"
require "models/parrot"
require "models/pirate"
require "models/project"
require "models/ship"
require "models/ship_part"
require "models/tag"
@ -1774,3 +1775,21 @@ def test_after_save_callback_with_autosave
assert_equal 1, comment.post_comments_count
end
end
class TestAutosaveAssociationOnAHasManyAssociationDefinedInSubclassWithAcceptsNestedAttributes < ActiveRecord::TestCase
def test_should_update_children_when_asssociation_redefined_in_subclass
agency = Agency.create!(name: "Agency")
valid_project = Project.create!(firm: agency, name: "Initial")
agency.update!(
"projects_attributes" => {
"0" => {
"name" => "Updated",
"id" => valid_project.id
}
}
)
valid_project.reload
assert_equal "Updated", valid_project.name
end
end

@ -122,6 +122,12 @@ class RestrictedWithErrorFirm < Company
has_many :companies, -> { order("id") }, foreign_key: "client_of", dependent: :restrict_with_error
end
class Agency < Firm
has_many :projects, foreign_key: :firm_id
accepts_nested_attributes_for :projects
end
class Client < Company
belongs_to :firm, foreign_key: "client_of"
belongs_to :firm_with_basic_id, class_name: "Firm", foreign_key: "firm_id"