Delete_All VS Destroy_All

delete_all vs destroy_all?

You are right. If you want to delete the User and all associated objects -> destroy_all
However, if you just want to delete the User without suppressing all associated objects -> delete_all

According to this post : Rails :dependent => :destroy VS :dependent => :delete_all

  • destroy / destroy_all: The associated objects are destroyed alongside this object by calling their destroy method
  • delete / delete_all: All associated objects are destroyed immediately without calling their :destroy method

What does User.destroy_all or User.delete_all do?

delete_all is from activerecord library not from FactoryGirl.

And the difference between these two is :

delete_all(conditions = nil) public

  • 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 :dependent rules defined on associations are not honored.
  • Returns the number of rows affected.

destroy_all(conditions = nil) public

  • Destroys the records matching conditions by instantiating each record and calling its destroy method.
  • Each object’s callbacks are executed (including :dependent association options and before_destroy/after_destroy Observer methods).
  • Returns the collection of objects that were destroyed; each will be frozen, to reflect that no changes should be made (since they can’t be persisted).

Note

Instantiation, callback execution, and deletion of each record can be time consuming when you’re removing many records at once. It generates at least one SQL DELETE query per record . If you want to delete many rows quickly, without concern for their associations or callbacks, use delete_all instead.

Are there destroy_all!/delete_all! in Rails?

No, there are no methods called delete_all! or destroy_all!. See the documentation.

Use delete_all or destroy_all instead. These methods will raise ActiveRecord errors if deletion fails. It means your transaction will be rolled back in case of errors as you expected.

How would I destroy_all or delete_all records except one in ruby on rails?

This worked for me in the end.

Message.where('id != ? AND parent_id = ?', parent_id, parent_id).where(:sender_status => 1, :recipient_status => 1).delete_all

Basically returns all messages of that particular conversation except the one where id == parent_id. Whenever id == parent_id this means it's a parent message.

destroy vs destroy_all

This will destroy all books. All of them.

has_many books, :dependent => :destroy

An important thing to remember is that :dependent => :destroy will cause the calling of the #destroy method in each and every one of the associated Books. By calling #destroy on each Book, any before_destroy or after_destroy callback will be executed for each Book.

Naturally, if you have a LOT of dependent books, this process could be expensive.

The :destroy_all is invalid, maybe you were thinking about :delete_all. The difference with :delete_all (instead of just :destroy) is that Rails will issue a single SQL statement to delete all dependent book records. No #destroy method will be called on any Book record, and no before_destroy or after_destroy callback will be executed.

The upside is that a single SQL statement is many times more efficient to delete records from the database than calling #destroy on each one of them.

This is very important to know. If you have any *_destroy callbacks on the Book model, you should be aware that defining :dependent => :delete_all will have the effect of ignoring any callbacks you defined on the Book model.

How to preview a delete_all or destroy_all query in Rails

Just off the top of my head, you could run the console in sandbox mode and run the delete query to see the sql. The changes would just be rolled back on exit.

Rails: Delete associated records on object destroy

The options dependent: :destroy is ignored when using with the :through (see doc). You have to do it manually, with a after_destroy callback for example.

 class Deal

after_destroy :destroy_coupon_codes

private

def destroy_coupon_codes
self.coupon_codes.destroy_all
end
end

What's a proper way to implement delete all option on a basic index form

I would pass an array of post IDs only if selected posts need to be deleted. If you want to delete all posts for a particular user, then here's how I would approach it:

config/routes.rb

resources :users do
resources :posts do
delete :destroy_all, on: :collection
end
end

Here, on: :collection means that the route applies to the collection of posts; the route therefore looks like this:

/users/:user_id/posts/destroy_all

You can read more about adding member and collection routes in the Rails Guides:

http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

app/controllers/posts_controller.rb

def destroy_all
user = User.find(params[:user_id])
user.posts.destroy_all
# redirect somewhere
end

app/views/posts/index.html.erb

<%= link_to(
"Delete all posts!",
destroy_all_user_posts_path,
method: :delete
) %>

If you want to delete all posts for the current_user, modify like so:

config/routes.rb

resources :posts do
delete :destroy_all, on: :collection
end

app/controllers/posts_controller.rb

def destroy_all
current_user.posts.destroy_all
# redirect somewhere
end

app/views/posts/index.html.erb

<%= link_to(
"Delete all posts!",
destroy_all_posts_path,
method: :delete
) %>

Hope that helps.

How to Destroy multiple objects simultaneously in Rails 3

destroy_all destroys the records matching conditions by calling destroy method for each instantiating record. So object’s callbacks are executed.

Model.destroy_all(:status => "inactive")
Model.where(:id => [1,2,3,4,5]).destroy_all
Model.where(:id => 1..5).destroy_all

UPDATE

User.where(:id => params[:ids]).destroy_all

/users?ids[]=1&ids[]=2&ids[]=3


Related Topics



Leave a reply



Submit