This project is archived and is in readonly mode.

#1775 ✓invalid
Stefan Kroes

Unexpected behaviour of named_scope with :conditions and :include options

Reported by Stefan Kroes | January 17th, 2009 @ 02:28 PM | in 3.x

When I specify both conditions and a include for a named scope, the conditions are in at least some cases applied to the included relation as well. This behavior seems strange to me. I'll give an example:

class Category < ActiveRecord::Base

  belongs_to :parent, :class_name => 'Category'

  has_many :children, :class_name => 'Category', :foreign_key => :parent_id

  named_scope :root_categories, :conditions => {:parent_id => nil}, :include => :children


In this example Category.root_categories will return all root categories but calling the children method on any of these will result in an empty array, even though children for these records exist in the database (when I omit the include option children are returned). The problem became apparent from looking at the log, which showed that AR had generated the following query to fetch the include relation:

SELECT `categories`.* 
FROM `categories` 
WHERE (`categories`.parent_id IN (130,180,955, etc. etc.)) 
AND (`categories`.`parent_id` IS NULL) 
ORDER BY name;

Comments and changes to this ticket

  • x@ES

    x@ES September 18th, 2009 @ 02:02 PM

    • Tag changed from :conditions, :include, activerecord, named_scope to named_scope

    in rails 2.3.4 all ok

  • x@ES

    x@ES September 18th, 2009 @ 02:05 PM

    • Tag changed from named_scope to :conditions, :include, activerecord, named_scope

    ups... restore original tags

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Dan Pickett

    Dan Pickett May 9th, 2010 @ 05:44 PM

    • Tag changed from :conditions, :include, activerecord, named_scope to :conditions, :include, activerecord, bugmash, named_scope
  • Rohit Arondekar

    Rohit Arondekar May 15th, 2010 @ 10:42 AM

    Not reproducible on rails 3 beta 3 on ruby 1.9.2dev (2010-05-08 trunk 27665)

    The children of the root_categories were retrieved. The following sql queries in the log entry confirms this:

    Category Load (1.1ms) SELECT "categories". FROM "categories" WHERE "categories"."parent_id" IS NULL)
    Category Load (0.5ms) SELECT "categories".
    FROM "categories" WHERE ("categories".parent_id IN (1,2,3,4,5,6))

    I've attached a rails app created using rails 3 beta 3, do the following to confirm:
    rake db:migrate
    rake db:seed
    rails server

    Navigate to localhost:3000/categories/index and note that category 1 and 4 have children being displayed.

    P.S I've used scope instead of named_scope, but I tried both and got the same result.

  • Anil Wadghule

    Anil Wadghule May 15th, 2010 @ 11:09 AM

    Verified app provided by Rohit. This issue is not reproducible with latest rails head (58adc673712).

  • Rohit Arondekar

    Rohit Arondekar June 24th, 2010 @ 11:45 AM

    • State changed from “new” to “invalid”

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