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.

Eager loading doesn't respect :order of association

#3670

Just want to clarify that this old ticket hasn't been resolved: http://dev.rubyonrails.org/ticket/3438

I couldn't find any other here in LH so just keep tracking.

Reported by Edgar Suarez · January 8th, 2010 @ 04:51 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

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

    Can't reproduce on master or 2.3.8(confirmed by dtrasbo in #railsbridge).

    June 23rd, 2010 @ 02:47 PM

  2. David Trasbo
    David Trasbo

    This has been fixed on Rails 2.3.8:

    ➜  rails_bugs  rails -v
    Rails 2.3.8
    ➜  rails_bugs  rails eager_loading_order
    ➜  rails_bugs  cd eager_loading_order
    ➜  eager_loading_order  script/generate model Foo
    ➜  eager_loading_order  script/generate model Bar foo_id:integer baz:string
    ➜  eager_loading_order  rake db:migrate
    (in /Users/dtrasbo/code/rails_bugs/eager_loading_order)
    
    class Foo < ActiveRecord::Base
      has_many :bars, :order => :baz
    end
    
    class Bar < ActiveRecord::Base
      belongs_to :foo
    end
    
    ➜  eager_loading_order  script/console 
    Loading development environment (Rails 2.3.8)
    ruby-1.8.7-p174 > Bar.create(:baz => 'c')
     => #<Bar id: 1> 
    ruby-1.8.7-p174 > Bar.create(:baz => 'b')
     => #<Bar id: 2> 
    ruby-1.8.7-p174 > Bar.create(:baz => 'a')
     => #<Bar id: 3> 
    ruby-1.8.7-p174 > Foo.create(:bars => Bar.all)
     => #<Foo id: 1> 
    ruby-1.8.7-p174 > f = Foo.first
     => #<Foo id: 1> 
    ruby-1.8.7-p174 > f.bars
     => [#<Bar id: 3, foo_id: 1, baz: "a", ...>, #<Bar id: 2, foo_id: 1, baz: "b", ...>, #<Bar id: 1, foo_id: 1, baz: "c", ...>]
    ruby-1.8.7-p174 > exit
    

    Restarted console here...

    ➜  eager_loading_order  script/console
    Loading development environment (Rails 2.3.8)
    ruby-1.8.7-p174 > foos = Foo.all(:include => :bars)
     => [#<Foo id: 1>] 
    ruby-1.8.7-p174 > foos.first.bars
     => [#<Bar id: 3, foo_id: 1, baz: "a", ...>, #<Bar id: 2, foo_id: 1, baz: "b", ...>, #<Bar id: 1, foo_id: 1, baz: "c", ...>]
    

    This ticket can be closed.

    June 23rd, 2010 @ 02:47 PM

  3. Valentine Bichkovsky
    Valentine Bichkovsky
    • Importance changed from to Low

    I've spent a couple of hours on this (or similar) problem today.
    Rails version: 2.3.5 and 2.3.8.
    Sample code:

    class RentAgreement < ActiveRecord::Base
      has_many :agreement_changes, :order => :date_from
      belongs_to :shop
    
      def some_code_expecting_agreement_changes_to_be_ordered_by_date_from
        ...
      end
    end
    

    In some reporting class:

    agreements = Agreement.all(:include => [:shops, :agreement_changes], :order => 'shops.title')
    

    It produces SQL query with "ORDER BY shops.title" only. So, Agreements' AgreementChanges are no longer ordered.
    Maybe this isn't a bug, but I think it should be at least noted in the documentation, that using ordering with eager loading ignores :order parameter, specified in :has_many.

    June 26th, 2010 @ 10:58 PM