Permalink
Browse files

allow reorder to affect eager loading correctly

  • Loading branch information...
1 parent 4af5252 commit 18fb2d4c022c1d95dc9a40493adbc3880da70dcd Matt Jones + Scott Walker committed Dec 28, 2011
@@ -249,7 +249,7 @@ def apply_join_dependency(relation, join_dependency)
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
@@ -57,6 +57,16 @@ def test_should_count_distinct_results
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

0 comments on commit 18fb2d4

Please sign in to comment.