1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #4216 from edgecase/master_fix_reorder_with_limited_ids

allow reorder to affect eager loading correctly
This commit is contained in:
Aaron Patterson 2011-12-28 17:03:37 -08:00
commit 780a222dc2
2 changed files with 11 additions and 1 deletions

View file

@ -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

View file

@ -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