This project is archived and is in readonly mode.
Eager loading doesn't respect :order of association
-
Rohit Arondekar
- State changed from new to invalid
Can't reproduce on master or 2.3.8(confirmed by dtrasbo in #railsbridge).
-
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 > exitRestarted 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
- 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 endIn 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.