1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #23570 from rthbound/addresses-23568

Addresses #23568, Incorrect error message with accepts_nested_attributes_for / has_many & has_one
This commit is contained in:
Matthew Draper 2016-02-18 07:45:22 +10:30
commit 011711ecc9
2 changed files with 14 additions and 8 deletions

View file

@ -29,14 +29,6 @@ module ActiveRecord
assign_multiparameter_attributes(multi_parameter_attributes) unless multi_parameter_attributes.empty? assign_multiparameter_attributes(multi_parameter_attributes) unless multi_parameter_attributes.empty?
end end
# Tries to assign given value to given attribute.
# In case of an error, re-raises with the ActiveRecord constant.
def _assign_attribute(k, v) # :nodoc:
super
rescue ActiveModel::UnknownAttributeError
raise UnknownAttributeError.new(self, k)
end
# Assign any deferred nested attributes after the base attributes have been set. # Assign any deferred nested attributes after the base attributes have been set.
def assign_nested_parameter_attributes(pairs) def assign_nested_parameter_attributes(pairs)
pairs.each { |k, v| _assign_attribute(k, v) } pairs.each { |k, v| _assign_attribute(k, v) }

View file

@ -61,6 +61,13 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase
assert_equal "No association found for name `honesty'. Has it been defined yet?", exception.message assert_equal "No association found for name `honesty'. Has it been defined yet?", exception.message
end end
def test_should_raise_an_UnknownAttributeError_for_non_existing_nested_attributes
exception = assert_raise ActiveModel::UnknownAttributeError do
Pirate.new(:ship_attributes => { :sail => true })
end
assert_equal "unknown attribute 'sail' for Ship.", exception.message
end
def test_should_disable_allow_destroy_by_default def test_should_disable_allow_destroy_by_default
Pirate.accepts_nested_attributes_for :ship Pirate.accepts_nested_attributes_for :ship
@ -582,6 +589,13 @@ module NestedAttributesOnACollectionAssociationTests
assert_respond_to @pirate, association_setter assert_respond_to @pirate, association_setter
end end
def test_should_raise_an_UnknownAttributeError_for_non_existing_nested_attributes_for_has_many
exception = assert_raise ActiveModel::UnknownAttributeError do
@pirate.parrots_attributes = [{ peg_leg: true }]
end
assert_equal "unknown attribute 'peg_leg' for Parrot.", exception.message
end
def test_should_save_only_one_association_on_create def test_should_save_only_one_association_on_create
pirate = Pirate.create!({ pirate = Pirate.create!({
:catchphrase => 'Arr', :catchphrase => 'Arr',