Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destroy all records for a table including :dependent records in other tables

What is the best way to do this on Heroku? through the console? I do have 'dependent: :destroy' set for all the dependent tables.

like image 472
Kevin K Avatar asked Oct 30 '12 18:10

Kevin K


People also ask

How do I delete a dependent table in SQL?

Right-click the Customers table in SQL Server Object Explorer, and select Delete. In the Preview Database Updates dialog, under User Action, notice that SSDT has identified all the dependent objects, in this case, a foreign key reference that will be dropped. Click Update Database.

Can we delete a row from a table based on another table I said yes?

You may wish to delete records in one table based on values in another table. Since you can't list more than one table in the FROM clause when you are performing a delete, you can use the EXISTS clause.

What is the difference between delete and destroy in rails?

Rails Delete operation using delete method Unlike the destroy method, with delete, you can remove a record directly from the database. Any dependencies to other records in the model are not taken into account. The method delete only deletes that one row in the database and nothing else.


2 Answers

heroku run rails console

> Model.destroy_all

The Model is the name of the model you want to destroy

like image 54
Manjunath Manoharan Avatar answered Oct 17 '22 06:10

Manjunath Manoharan


You may use:

Object.delete_all 

you can also set the :dependent option to :delete_all. :delete_all will issue a single SQL statement to delete all child records. because of this using :delete_all may give you better performance.

has_many :childs, :dependent => :delete_all
like image 30
felipeclopes Avatar answered Oct 17 '22 06:10

felipeclopes