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.

[PATCH] SQL generated by find ignores :include option

#2998

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?)

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

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

Activity

  1. Emery Finkelstein
    Emery Finkelstein

    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} }
    

    August 5th, 2009 @ 10:27 PM

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

    September 25th, 2009 @ 12:58 PM

  3. Elad Meidar
    Elad Meidar

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

    September 26th, 2009 @ 01:43 AM

  4. Luciano G. Panaro
    Luciano G. Panaro

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

    September 26th, 2009 @ 04:35 PM

  5. Luciano G. Panaro
    Luciano G. Panaro

    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
    

    September 26th, 2009 @ 05:20 PM

  6. John Trupiano
    John Trupiano

    +1 verified

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

    September 26th, 2009 @ 10:36 PM

  7. John Trupiano
    John Trupiano

    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.

    September 27th, 2009 @ 12:55 AM

  8. Elad Meidar
    Elad Meidar

    +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).

    September 27th, 2009 @ 04:17 AM

  9. sr.iniv.t
    sr.iniv.t

    +1 verified.

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

    September 27th, 2009 @ 05:03 AM

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

    September 27th, 2009 @ 11:48 AM

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

    September 27th, 2009 @ 12:33 PM

  12. John Trupiano
    John Trupiano
    • 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).

    September 29th, 2009 @ 12:56 PM

  13. iGEL
    iGEL
    • 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?

    October 5th, 2009 @ 08:41 PM

  14. John Trupiano
    John Trupiano

    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.

    October 5th, 2009 @ 08:51 PM

  15. Henrik Nyh
    Henrik Nyh

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

    January 14th, 2010 @ 12:01 PM

  16. Henrik Nyh
    Henrik Nyh

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

    January 14th, 2010 @ 01:03 PM

  17. Rizwan Reza
    Rizwan Reza
    • 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

    May 15th, 2010 @ 06:45 PM

  18. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:38 PM

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

    February 2nd, 2011 @ 04:38 PM