Deleting Users with Rails Console
I want to delete some users and duplicate tags that are in my db. Is there a way I can use rails console to list all of these objects so I can pinpoint eac开发者_JAVA技巧h one to delete them. They are not necessarily the last entries?
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)
That pp
stands for pretty print. Another sometimes handy is y
which prints stuff in Yaml format.
精彩评论