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.

named_scope gets applied to self-referential relations

#4132

When performing a find using a named scope on an object which utilizes acts_as_tree, the scope is getting applied to the acts_as_tree relations in addition to the primary find request.

Example:

class Job < ActiveRecord::Base
  acts_as_tree

  named_scope :bases, :conditions => { :parent_id => nil }
end

Performing:

Job.bases.destroy_all

Will only destroy the base jobs and not their Job#children jobs. Here is the log output:

Job Load (1.5ms)   SELECT * FROM "jobs" WHERE ("jobs"."parent_id" IS NULL) 
SQL (0.2ms)   BEGIN
Job Load (7.6ms)   SELECT * FROM "jobs" WHERE ("jobs".parent_id = 1) AND ("jobs"."parent_id" IS NULL)
Job Destroy (0.2ms)   DELETE FROM "jobs" WHERE "id" = 1

Note that the automatically generated query of children is getting the bases scope added to it which will return zero records since a job's parent_id cannot be equal to 1 AND not null.

Had I directly queried the jobs WITHOUT a named scope, the automatic destruction of the children would happen as expected.

Reported by elDub · March 8th, 2010 @ 07:42 PM

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

Activity

  1. José Valim
    José Valim

    Could you please provide a failing test case? Thanks.

    March 8th, 2010 @ 10:55 PM

  2. elDub
    elDub

    Here is a failing test case based off the v2.3.5 tag.

    March 9th, 2010 @ 03:20 AM

  3. elDub
    elDub
    • Tag changed from named_scope acts_as_tree to named_scope

    March 24th, 2010 @ 01:34 PM

  4. Alex Tomlins
    Alex Tomlins
    • Importance changed from to Low

    I've also run into this bug. It's not related to acts_as_tree, it seems to happen when calling destroy_all on a named_scope on an association.

    I've created a minimal app to illustrate the problem here: https://gist.github.com/2a208d1e03ce0d0e0fef

    June 29th, 2010 @ 09:13 AM

  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 2nd, 2011 @ 05:01 PM

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

    February 2nd, 2011 @ 05:01 PM