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.

2.3.9 destroy_all destroys loaded records that do not match the scope on associations

#5568

Release 2.3.9's commit https://github.com/rails/rails/commit/b41c3ba154c2038ecc7b230693662... introduced a nasty bug that causes some_association.some_scope.destroy_all to destroy any records that were loaded in some_association, even if they do not match some_scope.

For example, see the attached test case, where I create some records on an association, and then destroy_all on a scope of that association.

In ActiveRecord::Associations::AssociationCollection#load_target, the new code adds back all the records in @target, which are then returned in the target array to destroy_all, which then destroys them.

This didn't previously happen as that method previously only added back in any new records. So I have attached a patch that solves this particular case by only adding back the new records. This restores the prior behavior in this respect

One could argue though that this whole method needs a rewrite to have clearer semantics with respect to scopes.

Reported by Will Bryant · September 7th, 2010 @ 10:32 AM

State: stale
Milestone: none
Assigned to: Pratik Pratik
Importance: Low

Activity

  1. Will Bryant
  2. Will Bryant
    Will Bryant
    • Assigned user set to Pratik

    Pratik, would you mind taking a look at this? Hoping you know a lot more about the original patch than me :).

    September 7th, 2010 @ 10:41 AM

  3. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag changed from activerecord regression destroy_all load_target has_many named_scope to activerecord, destroy_all, has_many, named_scope, patch, regression
    • Importance changed from to Low

    October 10th, 2010 @ 09:02 AM

  4. Gonçalo Silva
    Gonçalo Silva

    Just confirmed this behaviour on Rails 2.3.9.

    October 18th, 2010 @ 05:14 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 9th, 2011 @ 12:31 AM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 9th, 2011 @ 12:31 AM