Last active
December 18, 2015 14:08
-
-
Save joshwand/5794515 to your computer and use it in GitHub Desktop.
activerecord patch (doesn't work!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb | |
index e53e855..3f27a34 100644 | |
--- a/activerecord/lib/active_record/nested_attributes.rb | |
+++ b/activerecord/lib/active_record/nested_attributes.rb | |
@@ -480,6 +480,13 @@ module ActiveRecord | |
if !call_reject_if(association_name, attributes) | |
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) | |
end | |
+ elsif association.klass.exists?(attributes['id']) | |
+ if !call_reject_if(association_name, attributes) | |
+ existing_record = association.klass.find(attributes['id']) | |
+ # puts association.target.size # => 2 | |
+ association.add_to_target(existing_record) | |
+ # puts association.target.size # => 3 | |
+ end | |
else | |
raise_nested_attributes_record_not_found!(association_name, attributes['id']) | |
end | |
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb | |
index 2f89699..dd6f2fc 100644 | |
--- a/activerecord/test/cases/nested_attributes_test.rb | |
+++ b/activerecord/test/cases/nested_attributes_test.rb | |
@@ -747,6 +747,12 @@ module NestedAttributesOnACollectionAssociationTests | |
assert_equal ['Grace OMalley', 'Privateers Greed', 'Buccaneers Servant'].to_set, @pirate.reload.send(@association_name).map(&:name).to_set | |
end | |
+ def test_should_associate_existing_record_if_valid_id_supplied | |
+ @alternate_params[association_getter]['bah'] = { :id => @unaffliated_bird.id } | |
+ @pirate.update @alternate_params | |
+ assert_equal ['Grace OMalley', 'Privateers Greed', 'Petey Campbell'].to_set, @pirate.reload.send(@association_name).map(&:name).to_set | |
+ end | |
+ | |
def test_should_be_possible_to_destroy_a_record | |
['1', 1, 'true', true].each do |true_variable| | |
record = @pirate.reload.send(@association_name).create!(:name => 'Grace OMalley') | |
@@ -833,6 +839,7 @@ class TestNestedAttributesOnAHasManyAssociation < ActiveRecord::TestCase | |
@pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?") | |
@pirate.birds.create!(:name => 'Posideons Killer') | |
@pirate.birds.create!(:name => 'Killer bandita Dionne') | |
+ @unaffliated_bird = Bird.create!(:name => 'Petey Campbell') | |
@child_1, @child_2 = @pirate.birds | |
@@ -855,6 +862,7 @@ class TestNestedAttributesOnAHasAndBelongsToManyAssociation < ActiveRecord::Test | |
@pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?") | |
@pirate.parrots.create!(:name => 'Posideons Killer') | |
@pirate.parrots.create!(:name => 'Killer bandita Dionne') | |
+ @unaffliated_bird = Parrot.create!(:name => 'Petey Campbell') | |
@child_1, @child_2 = @pirate.parrots |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seems not to be saving!