Merge pull request #33454 from azbshiri/extend-affects-nested-attributes

Call build when extend with nested attributes defined
This commit is contained in:
Richard Schneeman 2018-08-04 19:05:02 -05:00 committed by GitHub
commit a2b9768792
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

@ -501,7 +501,7 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
if attributes["id"].blank?
unless reject_new_record?(association_name, attributes)
association.build(attributes.except(*UNASSIGNABLE_KEYS))
association.reader.build(attributes.except(*UNASSIGNABLE_KEYS))
end
elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes["id"].to_s }
unless call_reject_if(association_name, attributes)

@ -1094,3 +1094,15 @@ def setup
assert_equal ["Ship name can't be blank"], part.errors.full_messages
end
end
class TestNestedAttributesWithExtend < ActiveRecord::TestCase
setup do
Pirate.accepts_nested_attributes_for :treasures
end
def test_extend_affects_nested_attributes
pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?")
pirate.treasures_attributes = [{ id: nil }]
assert_equal "from extension", pirate.treasures[0].name
end
end

@ -17,7 +17,13 @@ class Pirate < ActiveRecord::Base
after_remove: proc { |p, pa| p.ship_log << "after_removing_proc_parrot_#{pa.id}" }
has_and_belongs_to_many :autosaved_parrots, class_name: "Parrot", autosave: true
has_many :treasures, as: :looter
module PostTreasuresExtension
def build(attributes = {})
super({ name: "from extension" }.merge(attributes))
end
end
has_many :treasures, as: :looter, extend: PostTreasuresExtension
has_many :treasure_estimates, through: :treasures, source: :price_estimates
has_one :ship