Permalink
Browse files

Use save_changes instead of save when updating existing associated ob…

…jects in the nested_attributes plugin

This should be significantly faster for the case where you are
displaying a complex form to the user, and they make no changes to
the existing associated records when they submit it.

This shouldn't have a negative effect on production code, unless
save_changes doesn't work correctly for the associated objects.
It may break test code that checks for specific SQL or columns in
the update statement.
  • Loading branch information...
1 parent 9738b08 commit fec5c0ad569f092a1e88c079cdab882eba679c84 @jeremyevans committed Oct 29, 2009
Showing with 4 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +1 −1 lib/sequel/plugins/nested_attributes.rb
  3. +1 −1 spec/extensions/nested_attributes_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Use save_changes instead of save when updating existing associated objects in the nested_attributes plugin (jeremyevans)
+
* Allow Model#save_changes to accept an option hash that is passed to save, so you can save changes without validating (jeremyevans)
* Make nested_attributes plugin add newly created objects to cached association array immediately (jeremyevans)
@@ -180,7 +180,7 @@ def nested_attributes_update(reflection, pk, attributes)
after_validation_hook{validate_associated_object(reflection, obj)}
# Don't need to validate the object twice if :validate association option is not false
# and don't want to validate it at all if it is false.
- after_save_hook{obj.save(:validate=>false)}
+ after_save_hook{obj.save_changes(:validate=>false)}
obj
end
end
@@ -346,7 +346,7 @@ def validate
al.set(:tags_attributes=>[{:id=>30, :name=>'T2'}, {:name=>'T3'}])
@mods.should == []
al.save
- @mods.should == [[:u, :albums, {:name=>'Al'}, '(id = 10)'], [:u, :tags, {:name=>'T2', :number=>10}, '(id = 30)'], [:is, :tags, {:name=>"T3"}, 1], [:i, :at, {:album_id=>10, :tag_id=>1}, 2]]
+ @mods.should == [[:u, :albums, {:name=>'Al'}, '(id = 10)'], [:u, :tags, {:name=>'T2'}, '(id = 30)'], [:is, :tags, {:name=>"T3"}, 1], [:i, :at, {:album_id=>10, :tag_id=>1}, 2]]
proc{al.set(:tags_attributes=>[{:id=>30, :name=>'T2', :number=>3}])}.should raise_error(Sequel::Error)
proc{al.set(:tags_attributes=>[{:name=>'T2', :number=>3}])}.should raise_error(Sequel::Error)
end

0 comments on commit fec5c0a

Please sign in to comment.