Skip to content

Commit

Permalink
Use save_changes instead of save when updating existing associated ob…
Browse files Browse the repository at this point in the history
…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
jeremyevans committed Oct 29, 2009
1 parent 9738b08 commit fec5c0a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/plugins/nested_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/extensions/nested_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fec5c0a

Please sign in to comment.