Skip to content

Instantly share code, notes, and snippets.

@joshwand
Last active December 18, 2015 14:08
Show Gist options
  • Save joshwand/5794515 to your computer and use it in GitHub Desktop.
Save joshwand/5794515 to your computer and use it in GitHub Desktop.
activerecord patch (doesn't work!)
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
@joshwand
Copy link
Author

seems not to be saving!

$ ruby -Itest test/cases/nested_attributes_test.rb
Using sqlite3
Run options: --seed 19612

# Running:

..................................................................F............................F..................................................

Finished in 4.862508s, 30.0257 runs/s, 50.1799 assertions/s.

  1) Failure:
TestNestedAttributesOnAHasAndBelongsToManyAssociation#test_should_associate_existing_record_if_valid_id_supplied [test/cases/nested_attributes_test.rb:753]:
--- expected
+++ actual
@@ -1 +1 @@
-#<Set: {"Grace OMalley", "Privateers Greed", "Petey Campbell"}>
+#<Set: {"Grace OMalley", "Privateers Greed"}>



  2) Failure:
TestNestedAttributesOnAHasManyAssociation#test_should_associate_existing_record_if_valid_id_supplied [test/cases/nested_attributes_test.rb:753]:
--- expected
+++ actual
@@ -1 +1 @@
-#<Set: {"Grace OMalley", "Privateers Greed", "Petey Campbell"}>
+#<Set: {"Grace OMalley", "Privateers Greed"}>


146 runs, 244 assertions, 2 failures, 0 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment