diff options
author | benedikt <benedikt@synatic.net> | 2011-06-09 13:10:20 +0200 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-06-12 18:19:21 +0100 |
commit | fa8dfad7654baa831c8de515118008b3382df515 (patch) | |
tree | 56ea9c703cb58a49f6aa2e54dab2632a27a0922c /activerecord/lib | |
parent | 1fa059cd017c134499835593ba29620c4cd7358c (diff) | |
download | rails-fa8dfad7654baa831c8de515118008b3382df515.tar.gz rails-fa8dfad7654baa831c8de515118008b3382df515.tar.bz2 rails-fa8dfad7654baa831c8de515118008b3382df515.zip |
Don't wrap operations on collection associations in transactions when they are not needed, so the connection adapter does not send empty BEGIN COMMIT transactions blocks to the database.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 7e1a41e84d..7a8c0bf59f 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -117,7 +117,7 @@ module ActiveRecord result = true load_target if owner.new_record? - transaction do + block = lambda do records.flatten.each do |record| raise_on_type_mismatch(record) add_to_target(record) do |r| @@ -126,6 +126,8 @@ module ActiveRecord end end + owner.new_record? ? block.call : transaction(&block) + result && records end @@ -295,7 +297,7 @@ module ActiveRecord other_array.each { |val| raise_on_type_mismatch(val) } original_target = load_target.dup - transaction do + block = lambda do delete(target - other_array) unless concat(other_array - target) @@ -304,6 +306,8 @@ module ActiveRecord "new records could not be saved." end end + + owner.new_record? ? block.call : transaction(&block) end def include?(record) @@ -444,7 +448,7 @@ module ActiveRecord records.each { |record| raise_on_type_mismatch(record) } existing_records = records.reject { |r| r.new_record? } - transaction do + block = lambda do records.each { |record| callback(:before_remove, record) } delete_records(existing_records, method) if existing_records.any? @@ -452,6 +456,8 @@ module ActiveRecord records.each { |record| callback(:after_remove, record) } end + + existing_records.any? ? transaction(&block) : block.call end # Delete the given records from the association, using one of the methods :destroy, |