aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation.rb
diff options
context:
space:
mode:
authorAbhay Nikam <nikam.abhay1@gmail.com>2019-02-18 21:42:09 +0530
committerAbhay Nikam <nikam.abhay1@gmail.com>2019-02-19 16:57:49 +0530
commit5c8d4c3466cbfa850a89f718ec358ed3a352c34e (patch)
treee5629e9d01480426fd8ee1599fbd4efc8564b28c /activerecord/lib/active_record/relation.rb
parent84bd9adafde36d0192849649b86ffcd4e59e5e11 (diff)
downloadrails-5c8d4c3466cbfa850a89f718ec358ed3a352c34e.tar.gz
rails-5c8d4c3466cbfa850a89f718ec358ed3a352c34e.tar.bz2
rails-5c8d4c3466cbfa850a89f718ec358ed3a352c34e.zip
Introduce delete_by and destroy_by methods to ActiveRecord::Relation
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r--activerecord/lib/active_record/relation.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index feaa20ccc6..f6b21eaa3a 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -507,6 +507,32 @@ module ActiveRecord
affected
end
+ # Finds and destroys all records matching the specified conditions.
+ # This is short-hand for <tt>relation.where(condition).destroy_all</tt>.
+ # Returns the collection of objects that were destroyed.
+ #
+ # If no record is found, returns empty array.
+ #
+ # Person.destroy_by(id: 13)
+ # Person.destroy_by(name: 'Spartacus', rating: 4)
+ # Person.destroy_by("published_at < ?", 2.weeks.ago)
+ def destroy_by(*args)
+ where(*args).destroy_all
+ end
+
+ # Finds and deletes all records matching the specified conditions.
+ # This is short-hand for <tt>relation.where(condition).delete_all</tt>.
+ # Returns the number of rows affected.
+ #
+ # If no record is found, returns <tt>0</tt> as zero rows were affected.
+ #
+ # Person.delete_by(id: 13)
+ # Person.delete_by(name: 'Spartacus', rating: 4)
+ # Person.delete_by("published_at < ?", 2.weeks.ago)
+ def delete_by(*args)
+ where(*args).delete_all
+ end
+
# Causes the records to be loaded from the database if they have not
# been loaded already. You can use this if for some reason you need
# to explicitly load some records before actually using them. The