Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord `destroy_all` returns empty array instead of destroyed records

#5069

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

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

State: committed
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • 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.

    July 8th, 2010 @ 01:57 PM

  2. Mislav
    Mislav

    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.

    July 8th, 2010 @ 03:48 PM

  3. Mislav
    Mislav

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

    July 8th, 2010 @ 04:22 PM

  4. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to committed

    Cherry-picked. Thanks Mislav!

    July 8th, 2010 @ 07:37 PM