From 4ef75b63db314dce9e0c1a310ef3680b622bcf04 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Fri, 15 Feb 2013 08:57:33 +0100 Subject: don't apply invalid ordering when preloading hmt associations. closes #8663. When preloading a hmt association there two possible scenarios: 1.) preload with 2 queries: first hm association, then hmt with id IN () 2.) preload with join: hmt association is loaded with a join on the hm association The bug was happening in scenario 1.) with a normal order clause on the hmt association. The ordering was also applied when loading the hm association, which resulted in the error. This patch only applies the ordering the the hm-relation if we are performing a join (2). Otherwise the order will only appear in the second query (1). --- activerecord/test/cases/associations/eager_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test/cases/associations/eager_test.rb') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 3bf9125013..46f3c38ac5 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -73,6 +73,11 @@ class EagerAssociationTest < ActiveRecord::TestCase end end + def test_has_many_through_with_order + authors = Author.includes(:favorite_authors).to_a + assert_no_queries { authors.map(&:favorite_authors) } + end + def test_with_two_tables_in_from_without_getting_double_quoted posts = Post.select("posts.*").from("authors, posts").eager_load(:comments).where("posts.author_id = authors.id").order("posts.id").to_a assert_equal 2, posts.first.comments.size -- cgit v1.2.3