diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-19 21:47:22 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-19 21:52:49 +0900 |
commit | 64ef5e2f9e854a7f988227d629a699f11a998b17 (patch) | |
tree | ef239f8f8df88a6d200c94385f17fdb95681dac9 /activerecord/lib/active_record/relation.rb | |
parent | 30016df82f892f621c4937f3aec397f8d4cb30f2 (diff) | |
parent | 5c8d4c3466cbfa850a89f718ec358ed3a352c34e (diff) | |
download | rails-64ef5e2f9e854a7f988227d629a699f11a998b17.tar.gz rails-64ef5e2f9e854a7f988227d629a699f11a998b17.tar.bz2 rails-64ef5e2f9e854a7f988227d629a699f11a998b17.zip |
Merge pull request #35316 from abhaynikam/35304-add-delete_by_and_destroy_by
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.rb | 26 |
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 |