4 ms·
ActiveRecord::Base#delete_all allows you to delete directly in the database. For example: Person.delete_all("created_at < '2012-01-01'") will generate the
by sethco 14y ago
ActiveRecord::Base#delete_all allows you to delete directly in the database. For example:
Person.delete_all("created_at < '2012-01-01'")
will generate the sql command
"delete from persons where created_at < '2012-01-01'"
You can use normal AR conditions of course. For example:
Person.delete_all(["created_at < ?", Time.now.beginning_of_year])
That will not run any callbacks of course since the data is never being pulled back into your code and AR objects are not instantiated. If you need callbacks to run (for example to delete or update related objects) then look at the much slower ActiveRecord::Base#destroy_all.
- lobster_johnson 14y agoYou should be using the new ARel scoping mechanisms introduced in 3.x. See xentronium's comment (http://news.ycombinator.com/item?id=3935050 http://news.ycombinator.com/item?id=3935050).