You can clone with
So we nearly just hosed a table in production by trying:
Thankfully, we decided to read up on how delete_all works here and tested it locally, since it turns out that this simply deletes everything in the table.
Now I get that a LIMIT clause doesn't make sense on a DELETE op, but can we add a sensible translation in this case? I would expect it to behave like .destroy_all, which appears to instantiate the records and call #destroy on each. A #delete_all wouldn't have to inflate AR objects here, but instead, could simply query for the ids to delete, and then perform a DELETE FROM ... id IN (...) query.
Thoughts? I think we can prevent some future pain here.
Dave & John
We see the behavior using both Rails 3.1.3 and 3.0.11.
have you tried this?
Mode.delete_all(:limit => 100)
Sorry it will not work. It expects as a column.
I was going to have a crack at implementing this and naively thought that "DELETE FROM foos LIMIT " was valid SQL. However it appears to only work in MySQL, or as compile-time option for sqlite. No mention of it after a cursory glance at pg docs.
@daveyeu , your idea of getting the ids and then doing a "DELETE FROM foos WHERE id in (...)" I think would do what you'd expect Foo.limit(10).delete_all. It would be interesting to hear what rails contributors think.
Perhaps delete_all should raise an error if a limit is provided?
@jasonnoble that seems acceptable to me.
@tenderlove Any news? Maybe something like this:
def delete_all(conditions = nil)
raise ActiveRecordError.new("delete_all doesn't support limit scope") if self.limit_value
@daveyeu @arunagw Is destroy_all method serve that function?
delete_all raise an error if a limit is provided - fixes #4979
Fix #4979 against 3-2-stable - delete_all raise an error if a limit i…