Delete all in rails console Delete all in rails console ruby ruby

Delete all in rails console


You better use destroy because it goes through all the Rails magic (callbacks and such)

user.destroy #For a single recorduser.agents.destroy_all #For a collection


You are looking for a .destroy_all method. It destroys all records of a given collection.So user.agents.destroy_all, would return an empty array for user.agents.

You could not have used .delete_all because it is a class method and it deletes records that match a given condition. Like this, Agent.delete_all(condition). If used without a condition it deletes all records from a matched table.

Keep in mind that .destroy methods are instance methods. They instantiate an object and perform callbacks before erasing it. .delete methods are class methods and they directly erase an object.


This works for me

user.agents.find_each(&:destroy)