aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-12-28 17:03:37 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-12-28 17:04:23 -0800
commitf423bdc10b52e2f131f3596833d3a37f9773d820 (patch)
tree15af7b412d4c4fd57cc785729572a7d08d143b0e /activerecord
parent80f2eeb138c4a7901b7b015a6f56445902a9f1ab (diff)
downloadrails-f423bdc10b52e2f131f3596833d3a37f9773d820.tar.gz
rails-f423bdc10b52e2f131f3596833d3a37f9773d820.tar.bz2
rails-f423bdc10b52e2f131f3596833d3a37f9773d820.zip
Merge pull request #4216 from edgecase/master_fix_reorder_with_limited_ids
allow reorder to affect eager loading correctly
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb10
2 files changed, 11 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 3c8e0f2052..7fe50b93a1 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -249,7 +249,7 @@ module ActiveRecord
end
def construct_limited_ids_condition(relation)
- orders = relation.order_values.map { |val| val.presence }.compact
+ orders = (relation.reorder_value || relation.order_values).map { |val| val.presence }.compact
values = @klass.connection.distinct("#{@klass.connection.quote_table_name table_name}.#{primary_key}", orders)
relation = relation.dup
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 88d7d47aea..f1a341437f 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -57,6 +57,16 @@ class HasManyAssociationsTestForCountDistinctWithFinderSql < ActiveRecord::TestC
end
end
+class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
+ fixtures :authors, :posts, :comments
+
+ def test_should_generate_valid_sql
+ author = authors(:david)
+ # this can fail on adapters which require ORDER BY expressions to be included in the SELECT expression
+ # if the reorder clauses are not correctly handled
+ assert author.posts_with_comments_sorted_by_comment_id.where('comments.id > 0').reorder('posts.comments_count DESC', 'posts.taggings_count DESC').last
+ end
+end
class HasManyAssociationsTest < ActiveRecord::TestCase