I am trying to delete an array of users but the way I have it it is deleting one by one. Is there a better way to do it?
My code is:
@users ||= User.where("clicks_given - clicks_received < ?", -5).to_a
@users.each do |user|
user.destroy
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can just use Rails’ built-in methods. Note that you need to wrap your query in an array (if you’re interpolating variables) when using these methods.
To iterate over each one calling destroy (which will run callbacks, etc.):
Or to just delete these in the database in a single query (no iteration over each item), you can do this, but keep in mind it won’t run your callbacks: