This project is archived and is in readonly mode.

#3670 ✓invalid
Edgar Suarez

Eager loading doesn't respect :order of association

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

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.

Comments and changes to this ticket

  • Rohit Arondekar

    Rohit Arondekar June 23rd, 2010 @ 02:47 PM

    • State changed from “new” to “invalid”

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

  • David Trasbo

    David Trasbo June 23rd, 2010 @ 02:47 PM

    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.

  • Valentine Bichkovsky

    Valentine Bichkovsky June 26th, 2010 @ 10:58 PM

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

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>

Pages