views:

44

answers:

1

I want to clear a table in my railsapp , without dropping the database and migrating...

 MyModel.all.each{|m| m.destroy}

I would expect this code to delete every record in the my_model table, but this is not happening... using Rails 2.3.4 + MySQL 5.1

EDIT: the issue was based on the plugin better_nested_set which didn't allow me to delete the entries in that order

MyModel.delete_all worked on the other hand , maybe because it executes truncate on the database (?)

+5  A: 

Use MyModel.destroy_all to delete all the records for your model.

John Topley
fast and useful answer,thank you
Midday
the only caveat is that if you're doing anything with callbacks (before_destroy, after_destroy, etc.) this will bypass them. Otherwise it will do what you want and do so much more quickly than calling destroy on each instance.
semanticart
That is totally wrong, semanticart! It's the `delete_all` method that bypasses the callbacks.
John Topley