This project is archived and is in readonly mode.

#4132 ✓stale

named_scope gets applied to self-referential relations

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

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.


class Job < ActiveRecord::Base

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



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.

Comments and changes to this ticket

  • José Valim

    José Valim March 8th, 2010 @ 10:55 PM

    Could you please provide a failing test case? Thanks.

  • elDub

    elDub March 9th, 2010 @ 03:20 AM

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

  • elDub

    elDub March 24th, 2010 @ 01:34 PM

    • Tag changed from named_scope acts_as_tree to named_scope
  • Alex Tomlins

    Alex Tomlins June 29th, 2010 @ 09:13 AM

    • 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:

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • State changed from “open” to “stale”

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