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.

Unexpected behaviour of named_scope with :conditions and :include options

#1775

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

end

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;

Reported by Stefan Kroes · January 17th, 2009 @ 02:28 PM

State: invalid
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. x@ES
    x@ES
    • Tag changed from :conditions, :include, activerecord, named_scope to named_scope

    in rails 2.3.4 all ok

    September 18th, 2009 @ 02:02 PM

  2. x@ES
    x@ES
    • Tag changed from named_scope to :conditions, :include, activerecord, named_scope

    ups... restore original tags

    September 18th, 2009 @ 02:05 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  4. Dan Pickett
    Dan Pickett
    • Tag changed from :conditions, :include, activerecord, named_scope to :conditions, :include, activerecord, bugmash, named_scope

    May 9th, 2010 @ 05:44 PM

  5. Rohit Arondekar
    Rohit Arondekar

    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.

    May 15th, 2010 @ 10:42 AM

  6. Anil Wadghule
    Anil Wadghule

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

    May 15th, 2010 @ 11:09 AM

  7. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid

    June 24th, 2010 @ 11:45 AM