Permalink
Browse files

Added automatic transaction block around AssociationCollection.<<, As…

…sociationCollection.delete, and AssociationCollection.destroy_all

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@171 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 120649d commit 7500a5469231665e5d65f47ff75e55097f096e09 @dhh dhh committed Dec 15, 2004
Showing with 19 additions and 8 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +17 −8 activerecord/lib/active_record/associations/association_collection.rb
@@ -1,5 +1,7 @@
*CVS*
+* Added automatic transaction block around AssociationCollection.<<, AssociationCollection.delete, and AssociationCollection.destroy_all
+
* Fixed that Base#find will return an array if given an array -- regardless of the number of elements #270 [Marten]
* Fixed that has_and_belongs_to_many would generate bad sql when naming conventions differed from using vanilla "id" everywhere [RedTerror]
@@ -37,11 +37,14 @@ def reload
# Add +records+ to this association. Returns +self+ so method calls may be chained.
# Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically.
def <<(*records)
- flatten_deeper(records).each do |record|
- raise_on_type_mismatch(record)
- insert_record(record)
- @collection << record if loaded?
+ @owner.transaction do
+ flatten_deeper(records).each do |record|
+ raise_on_type_mismatch(record)
+ insert_record(record)
+ @collection << record if loaded?
+ end
end
+
self
end
@@ -51,13 +54,19 @@ def <<(*records)
# Remove +records+ from this association. Does not destroy +records+.
def delete(*records)
records = flatten_deeper(records)
- records.each { |record| raise_on_type_mismatch(record) }
- delete_records(records)
- records.each { |record| @collection.delete(record) } if loaded?
+
+ @owner.transaction do
+ records.each { |record| raise_on_type_mismatch(record) }
+ delete_records(records)
+ records.each { |record| @collection.delete(record) } if loaded?
+ end
end
def destroy_all
- each { |record| record.destroy }
+ @owner.transaction do
+ each { |record| record.destroy }
+ end
+
@collection = []
end

0 comments on commit 7500a54

Please sign in to comment.