Skip to content

Commit

Permalink
Fix accepts_nested_attributes for child classes
Browse files Browse the repository at this point in the history
Closes GH-8131
  • Loading branch information
Gabriel Sobrinho & Ricardo Henrique authored and sobrinho committed Nov 7, 2012
1 parent e41d78c commit 58e48d5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
26 changes: 26 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
7 changes: 4 additions & 3 deletions activerecord/lib/active_record/nested_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/nested_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

This comment has been minimized.

Copy link
@sobrinho

sobrinho Nov 8, 2012

Contributor

@rafaelfranca LOL.

Can you change that name?

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Nov 8, 2012

Member

💣

Sure. I messed this 😢

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Nov 8, 2012

Member

Done dbd0b12

This comment has been minimized.

Copy link
@caironoleto

caironoleto Nov 8, 2012

Contributor

always test rails

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
Expand Down

0 comments on commit 58e48d5

Please sign in to comment.