This project is archived and is in readonly mode.

#5069 ✓committed

ActiveRecord `destroy_all` returns empty array instead of destroyed records

Reported by Mislav | July 8th, 2010 @ 01:19 PM

Just noticed a regression in Rails 3: destroy_all always returns an empty array. I've added tests to master and 2-3-stable that destroy_all should return destroyed, frozen records same as destroy class method does. I've fixed the failure in Rails 3 and also fixed destroy_all on associations in both Rails 3 and 2.3.

  1. Fix for master is in mislav/destroy_all-fix
  2. Fix for 2.3 is in mislav/destroy_all-2-3-stable

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh July 8th, 2010 @ 01:57 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “Low”
    to_a.each {|object| object.destroy }.tap { reset }

    What if destroy operation fails on an object. In that case even the failed object will show up in the returned collection. I think changing 'each' to 'select' might fix that problem.

    If you don't mind can you add test for this edge condition. Thanks.

  • Mislav

    Mislav July 8th, 2010 @ 03:48 PM

    That wasn't a behavior of Rails 2.3. Why change it? A user can iterate over the "destroyed" objects collection and check which records are truly destroyed? and which have failed because of before_destroy callbacks.

    But you're right: whatever behavior is in place, it should be tested.

  • Mislav

    Mislav July 8th, 2010 @ 04:22 PM

    Heads-up to those who might have already pulled: I've just force-pushed something I forgot to my branch for master.

  • Jeremy Kemper

    Jeremy Kemper July 8th, 2010 @ 07:37 PM

    • State changed from “open” to “committed”

    Cherry-picked. Thanks Mislav!

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket