简体   繁体   中英

Delete a record from console — Ruby on Rails

I want to delete a record from the console.

I logged through script/console and

User.find(1).delete
$<User id: 1, ..............>
User.find(1)
ActiveRecord::RecordNotFound: Couldn't find User with ID=1
........
........
........

Everything looks fine till here.

But when I exit the console and return back to console by script/console command, I am able again to see the record with id = 1.

The database is not local and I'm connecting through database.yml by giving an ip.

when I try to do the same on the development local database, everything is fine.

Can someone please explain whats happening and where am I missing!

Thanks

Try

User.find(1).destroy

This should work in the console and otherwise.

On a similar note you can remove all the tuples for a given Object from the rails console as follows:

@object = Object.all

@object.each do |o|
  o.delete
end

Easy way to remove Models data from console

rails console

Store user model to an instance variable

user = User.find(1)

delete or destroy the relations by following commands

user.delete

or

user.destroy

Try User.count or y User.count

User.first where first is the number you want to delete

User.first.destroy

Make sure you aren't invoking console in sandbox mode, which will prevent the delete operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM