Permalink
Browse files

Fix accepts_nested_attributes for child classes

Closes GH-8131
  • Loading branch information...
1 parent e41d78c commit 58e48d5292242f000dc8a87fdbb1c0ccdcf286d8 Gabriel Sobrinho & Ricardo Henrique committed with sobrinho Nov 7, 2012
View
@@ -1,5 +1,31 @@
## Rails 3.2.9 (unreleased)
+* Fix issue that raises `NameError` when overriding the `accepts_nested_attributes` in child classes.
+
+ Before:
+
+ class Shared::Person < ActiveRecord::Base
+ has_one :address
+
+ accepts_nested_attributes :address, :reject_if => :all_blank
+ end
+
+ class Person < Shared::Person
+ accepts_nested_attributes :address
+ end
+
+ Person
+ #=> NameError: method `address_attributes=' not defined in Person
+
+ After:
+
+ Person
+ #=> Person(id: integer, ...)
+
+ Fixes #8131.
+
+ *Gabriel Sobrinho, Ricardo Henrique*
+
* Fix issue with collection associations calling first(n)/last(n) and attempting
to set the inverse association when `:inverse_of` was used. Fixes #8087.
@@ -277,13 +277,14 @@ def accepts_nested_attributes_for(*attr_names)
type = (reflection.collection? ? :collection : :one_to_one)
+ # remove_possible_method :pirate_attributes=
+ #
# def pirate_attributes=(attributes)
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes, mass_assignment_options)
# end
class_eval <<-eoruby, __FILE__, __LINE__ + 1
- if method_defined?(:#{association_name}_attributes=)
- remove_method(:#{association_name}_attributes=)
- end
+ remove_possible_method(:#{association_name}_attributes=)
+
def #{association_name}_attributes=(attributes)
assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, mass_assignment_options)
end
@@ -172,6 +172,17 @@ def test_first_and_array_index_zero_methods_return_the_same_value_when_nested_at
man.interests_attributes = [{:id => interest.id, :topic => 'gardening'}]
assert_equal man.interests.first.topic, man.interests[0].topic
end
+
+ def test_something
@sobrinho

sobrinho Nov 8, 2012

Contributor

@rafaelfranca LOL.

Can you change that name?

@rafaelfranca

rafaelfranca Nov 8, 2012

Owner

💣

Sure. I messed this 😢

+ Pirate.accepts_nested_attributes_for(:parrot)
+
+ mean_pirate_class = Class.new(Pirate) do
+ accepts_nested_attributes_for :parrot
+ end
+ mean_pirate = mean_pirate_class.new
+ mean_pirate.parrot_attributes = { :name => "James" }
+ assert_equal "James", mean_pirate.parrot.name
+ end
end
class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase

0 comments on commit 58e48d5

Please sign in to comment.