diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-20 18:33:14 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-20 18:33:14 +0530 |
commit | f216fadc0e4a54d1807fe5a9462f7bd34e9024b0 (patch) | |
tree | 8c47b27d0f1b0981b37b836103fcc3dc8ac48d1f /activerecord/lib/active_record/relation.rb | |
parent | 97568056763dc3bca7804a1426fae32a2031cfec (diff) | |
download | rails-f216fadc0e4a54d1807fe5a9462f7bd34e9024b0.tar.gz rails-f216fadc0e4a54d1807fe5a9462f7bd34e9024b0.tar.bz2 rails-f216fadc0e4a54d1807fe5a9462f7bd34e9024b0.zip |
Delegate delete_all to Relation
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index a253dbd19d..a6c283e03c 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -146,8 +146,25 @@ module ActiveRecord end end - def delete_all - arel.delete.tap { reset } + # Deletes the records matching +conditions+ without instantiating the records first, and hence not + # calling the +destroy+ method nor invoking callbacks. This is a single SQL DELETE statement that + # goes straight to the database, much more efficient than +destroy_all+. Be careful with relations + # though, in particular <tt>:dependent</tt> rules defined on associations are not honored. Returns + # the number of rows affected. + # + # ==== Parameters + # + # * +conditions+ - Conditions are specified the same way as with +find+ method. + # + # ==== Example + # + # Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')") + # Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else']) + # + # Both calls delete the affected posts all at once with a single DELETE statement. If you need to destroy dependent + # associations or call your <tt>before_*</tt> or +after_destroy+ callbacks, use the +destroy_all+ method instead. + def delete_all(conditions = nil) + conditions ? where(conditions).delete_all : arel.delete.tap { reset } end # Deletes the row with a primary key matching the +id+ argument, using a |