rails/rails — Issue #3672: :dependent => :delete_all malfunctioning in 3.1
Author: njakobsen
Created: 2011-11-17 23:36:49+00:00
State: open

Here's the set up

class Discussion
has_many :posts, :dependent => :delete_all
end

class Discussion
belongs_to :discussion
end

In Rails 3.0, calling Discussion.find(1).destroy will perform an sql delete as follows

DELETE FROM "posts" WHERE ((discussion_id = 1))
DELETE FROM "discussions" WHERE ("discussion"."id" = 1)

In Rails 3.1, calling the same thing will perform the following

Post Load (247.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."discussion_id" = 1
DELETE FROM "posts" WHERE "posts"."discussion_id" = 1 AND "posts"."id" IN (778876, 778928, 779125, 779175, 779223, 788836, etc...)

You can create a new Rails 3.1 app and this strange behaviour will happen. This is broken because if you have many posts, ruby eats your CPU alive.