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
By using destroy, you can delete the record from rails as well as its other existing dependencies. So in the context of our rails application, if we delete a book record using the destroy function, the authors associated with the book will also be deleted.
To delete a row at any location, call the deleteRows method of the Cells collection. The DeleteRows method takes two parameters: Row index, the index of the row from where the rows will be deleted. Number of rows, total number of rows that need to be deleted.
You can't explicitly destroy object. Ruby has automatic memory management. Objects no longer referenced from anywhere are automatically collected by the garbage collector built in the interpreter.
If you are looking for a way to it without SQL you should be able to use delete_all. See here for more information. The records are deleted without loading them first which makes it very fast but will break functionality like counter cache that depends on rails code to be executed upon deletion.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With