Rails, a given array of elements, how to delete in the console

Given the following:

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

which 15 for: @users.length

Is there a way in the rails console to delete all of these entries, perhaps by doing something like @users.delete?

+5
source share
6 answers

In your model class, there is a deletemethod (s destroy) that can take a single ID or an array of identifiers to delete (or destroy). Passing an array will produce only one statement delete, and this is the best option in this case.

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

, destroy ActiveRecord ( ..). , , destroy ( delete), , J -_- L answer .

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

- destroy_all delete_all:

@users.destroy_all
# OR
@users.delete_all

+13

@users.destroy_all

+2

Yep:

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

- . :

User.destroy_all(id: [2, 3])
+1

destroy_all delete_all : DELETE FROM USERS, ! ! , - User.delete @users.map { |u| u.id }

0

All Articles