Rails, given an array of items, how to delete in the console

@users = User.where(:state => 1)

The simplest way is to use destroy_all or delete_all:

@users.destroy_all
# OR
@users.delete_all

That's all


Your model class has a delete method (and a destroy method) that can take a single ID or a an Array of IDs to delete (or destroy). Passing an array will issue only a single DELETE statement, and is the best option in this case.

User.delete @users.map { |u| u.id }
# or
User.destroy @users.map { |u| u.id }

Note that when you call destroy on an ActiveRecord model, it creates an instance of the record before deleting it (so callbacks, etc. are run). In this case, since you already have fully instantiated objects, if you really do want to call destroy (instead of delete), it is probably more performant to use the method in J-_-L's answer and iterate over them yourself.


yep:

@users.each{ |u| u.destroy }