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 | |
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')
-rw-r--r-- | activerecord/lib/active_record/querying.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 26 |
2 files changed, 27 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index 2a27f53f06..b3eeb60571 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -7,7 +7,7 @@ module ActiveRecord delegate :first_or_create, :first_or_create!, :first_or_initialize, to: :all delegate :find_or_create_by, :find_or_create_by!, :create_or_find_by, :create_or_find_by!, :find_or_initialize_by, to: :all delegate :find_by, :find_by!, to: :all - delegate :destroy_all, :delete_all, :update_all, to: :all + delegate :destroy_all, :delete_all, :update_all, :destroy_by, :delete_by, to: :all delegate :find_each, :find_in_batches, :in_batches, to: :all delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins, :left_joins, :left_outer_joins, :or, :where, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly, :extending, 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 |