To truncate an ActiveRecord table, I can do
Category.destroy_all
or
Post.destroy_all
How does one go about truncating a categories_post table?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For a true
TRUNCATE, you can useexecuteto run the raw SQL.Your examples using models weren’t doing true
TRUNCATEqueries.destroy_alldoes notTRUNCATEa table. It “destroys the records matching conditions by instantiating each record and calling its destroy method” (link).delete_allis closer – it ignores callbacks – but still not aTRUNCATE.Using the
executemethod deletes the rows from the database without creating any model instances.Also, an actual
TRUNCATEquery, at least in MySQL, will reset the auto-increment on the primary key, so that the next record you insert will have id of 1.