aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/association_collection.rb
diff options
context:
space:
mode:
authorTobias Lütke <tobias.luetke@gmail.com>2005-10-14 12:53:39 +0000
committerTobias Lütke <tobias.luetke@gmail.com>2005-10-14 12:53:39 +0000
commit5fa8793f02b58509a90eed69e8dc3d199f89b4ad (patch)
treeb00feb5e67e67c43b20c600ad7bc62dfd98243be /activerecord/lib/active_record/associations/association_collection.rb
parent4a1ed01e9d53da87b57802cb99e3ca7956a6eef7 (diff)
downloadrails-5fa8793f02b58509a90eed69e8dc3d199f89b4ad.tar.gz
rails-5fa8793f02b58509a90eed69e8dc3d199f89b4ad.tar.bz2
rails-5fa8793f02b58509a90eed69e8dc3d199f89b4ad.zip
DRYed up Associations#clear. Closes #1906 [Caleb]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2580 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations/association_collection.rb')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 932e8e2aae..ced734cfcd 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -33,6 +33,12 @@ module ActiveRecord
alias_method :push, :<<
alias_method :concat, :<<
+
+ # Remove all records from this association
+ def delete_all
+ delete(@target)
+ @target = []
+ end
# Remove +records+ from this association. Does not destroy +records+.
def delete(*records)
@@ -50,6 +56,17 @@ module ActiveRecord
end
end
end
+
+ # Removes all records from this association. Returns +self+ so method calls may be chained.
+ def clear
+ return self if empty? # forces load_target if hasn't happened already
+ if @options[:exclusively_dependent]
+ destroy_all
+ else
+ delete_all
+ end
+ self
+ end
def destroy_all
@owner.transaction do