Removing Users Using the Rails Console

I want to remove some users and duplicate tags that are in my db. Is there a way I can use the rails console to list all of these objects so that I can pinpoint each of them to remove them. This is not necessarily the latest entries?

+8
ruby-on-rails-3
source share
1 answer

Assuming your model is derived from ActiveRecord::Base and named User , you can do with rails console

 pp User.all # all users 

or

 pp User.all(:conditions => {:firstname => 'fred'}) # use hash conditions 

or

 pp User.all(:conditions => "lastname LIKE 'jenkin%'") # use custom sql conditions 

and having the right user (say id 42) you can do

 User.delete(42) 

This pp means pretty printed. Another sometimes convenient is y , which prints material in Yaml format.

+26
source share

All Articles