This project is archived and is in readonly mode.

#2998 ✓stale
Emery Finkelstein

[PATCH] SQL generated by find ignores :include option

Reported by Emery Finkelstein | August 5th, 2009 @ 09:23 PM

Since upgrading to Rails 2.3.3 from 2.3.2 named scopes have started ignoring the :include option.

class Item < ActiveRecord::Base
has_many :collection_sets, :through => :set_items
has_many :set_items

named_scope :in_collection, lambda {|c|
    { :include =>  :collection_sets, :order => 'items.name', :conditions => 
       {:'collection_sets.collection_id' => c}
    }
}
end

Item.in_collection(2)

generates this SQL query in rails 2.3.3

SELECT * FROM `items` WHERE (`collection_sets`.`collection_id` = 2)  ORDER BY items.name

In Rails 2.3.2 it generated this SQL query

SELECT * FROM `items` LEFT OUTER JOIN `set_items` ON (`item`.`id` = `set_items`.`item_id`) LEFT OUTER JOIN `collection_sets` ON (`collection_sets`.`id` = `set_items`.`collection_set_id`) WHERE `collection_sets`.`id` = 2 ORDER BY items.name

Providing an unknown value for :include option in the named scope does not produce the expected ActiveREcord::ConfigurationError (Association named 'foo' was not found; perhaps you misspelled it?)

Comments and changes to this ticket

  • Emery Finkelstein

    Emery Finkelstein August 5th, 2009 @ 10:27 PM

    I've done a little more investigation into the issue and made the following observations.

    Giving the same options the named scope should have to scoped works as expected.
    eg:

    Item.scoped(:include =>  :collection_sets, :order => 'items.name', :conditions => {:'collection_sets.collection_id' => c})
    

    Works as expected when c is defined as either a CollectionSet object or an integer.

    A result of this is that I could create a lambda or Proc or method that just calls an anonymous scope, but this work around cannot be chained.

    proxy_options for the named scope includes :include.

    Item.in_collection(2).proxy_options => {:include=>:collection_sets, :order=>"items.name", :conditions=>{:"collection_sets.collection_id"=> 2} }
    
  • CancelProfileIsBroken

    CancelProfileIsBroken September 25th, 2009 @ 12:58 PM

    • Tag changed from 2.3.3, :include, activerecord, error, ignored, named_scope, sql to 2.3.3, :include, activerecord, bugmash, error, ignored, named_scope, sql
  • Elad Meidar

    Elad Meidar September 26th, 2009 @ 01:43 AM

    +1 verified on 2-3-stable, i tried to follow up named_scope.rb and try to point to where exactly the :include option is dropped/ignored when set within a lambda, but couldn't find any pointers..

  • Luciano G. Panaro

    Luciano G. Panaro September 26th, 2009 @ 04:35 PM

    +1 verified - will try to write a test for it

  • Luciano G. Panaro

    Luciano G. Panaro September 26th, 2009 @ 05:20 PM

    I understand that the options of a scope such as conditions are passed directly to #find. So in this case it would be a bug in #find rather than scopes themselves. This for example did not work either:

    def self.test(c)
      self.find :all, :include => :collection_sets, :conditions => { :'collection_sets.id' => c } 
    end
    
  • John Trupiano

    John Trupiano September 26th, 2009 @ 10:36 PM

    +1 verified

    I have attached a test case that demonstrates the error on the #find method (as per Luciano's finding).

  • John Trupiano

    John Trupiano September 27th, 2009 @ 12:55 AM

    So it turns out the culprit was using symbols for keys in the conditions hash. Had you conversely used a string instead, this query would have worked.

    I have updated my previous patch with the one line fix to associations.rb to allow symbols as keys.

  • Elad Meidar

    Elad Meidar September 27th, 2009 @ 04:17 AM

    +1 verified on 2-3-stable and Master, patch however, applies cleanly only on 2-3-stable.
    i've attached a patch for master, (credits are mine, i have no idea how to notate them to someone else, sorry).

  • sr.iniv.t

    sr.iniv.t September 27th, 2009 @ 05:03 AM

    +1 verified.

    Both patches apply clean (John's on 2-3-stable, Elad's on master) and all tests pass.

  • CancelProfileIsBroken

    CancelProfileIsBroken September 27th, 2009 @ 11:48 AM

    • Tag changed from 2.3.3, :include, activerecord, bugmash, error, ignored, named_scope, sql to 2.3.3, :include, activerecord, bugmash, bugmash-review, error, ignored, named_scope, sql
  • CancelProfileIsBroken

    CancelProfileIsBroken September 27th, 2009 @ 12:33 PM

    • Tag changed from 2.3.3, :include, activerecord, bugmash, bugmash-review, error, ignored, named_scope, sql to 2.3.3, :include, activerecord, bugmash-review, error, ignored, named_scope, sql
  • John Trupiano

    John Trupiano September 29th, 2009 @ 12:56 PM

    • Title changed from “named_scope ignores :include option” to “[PATCH] SQL generated by find ignores :include option”

    This patch is good to be applied. Just changing the name to include PATCH and better describe the actual patch (original bug report was just a derivative manifestation of the real bug).

  • iGEL

    iGEL October 5th, 2009 @ 08:41 PM

    • Tag changed from 2.3.3, :include, activerecord, bugmash-review, error, ignored, named_scope, sql to 2-3-stable, 2.3, 2.3.5, 3.0, :include, activerecord, bugmash-review, error, ignored, named_scope, patch, sql

    Any news?

  • John Trupiano

    John Trupiano October 5th, 2009 @ 08:51 PM

    I would love to see this one applied. Duplicate bugs (e.g. https://rails.lighthouseapp.com/projects/8994/tickets/3316-has_many... ) continue to be reported.

  • Henrik Nyh

    Henrik Nyh January 14th, 2010 @ 12:01 PM

    +1, just ran into this on 2.3.5 (after upgrading from 2.1.2 where it worked).

  • Henrik Nyh

    Henrik Nyh January 14th, 2010 @ 01:03 PM

    Made a monkeypatch initializer that can be used until Johns patch is merged: http://gist.github.com/277141

  • Rizwan Reza

    Rizwan Reza May 15th, 2010 @ 06:45 PM

    • Tag changed from 2-3-stable, 2.3, 2.3.5, 3.0, :include, activerecord, bugmash-review, error, ignored, named_scope, patch, sql to 2-3-stable, 2.3, 2.3.5, 3.0, :include, activerecord, bugmash, error, ignored, named_scope, patch, sql
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:38 PM

    • State changed from “new” to “open”
    • Tag changed from 2-3-stable, 2.3, 2.3.5, 3.0, :include, activerecord, bugmash, error, ignored, named_scope, patch, sql to 2-3-stable, 23, 235, 30, activerecord, bugmash, error, ignored, include, named_scope, patch, sql
    • Importance changed from “” to “”

    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 @ 04:38 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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages