diff --git a/lib/squeel/adapters/active_record/4.1/relation_extensions.rb b/lib/squeel/adapters/active_record/4.1/relation_extensions.rb index 38e9335..3f4cc7b 100644 --- a/lib/squeel/adapters/active_record/4.1/relation_extensions.rb +++ b/lib/squeel/adapters/active_record/4.1/relation_extensions.rb @@ -225,7 +225,9 @@ def build_from def build_order(arel) orders = order_visit(dehashified_order_values) orders = orders.uniq.reject(&:blank?) - orders = reverse_sql_order(orders) if reverse_order_value && !reordering_value + if reverse_order_value && (!reordering_value || Array(order_values).any?(&:present?)) + orders = reverse_sql_order(orders) + end arel.order(*orders) unless orders.empty? end diff --git a/spec/squeel/adapters/active_record/relation_extensions_spec.rb b/spec/squeel/adapters/active_record/relation_extensions_spec.rb index 0ea9667..71a95f9 100644 --- a/spec/squeel/adapters/active_record/relation_extensions_spec.rb +++ b/spec/squeel/adapters/active_record/relation_extensions_spec.rb @@ -930,6 +930,11 @@ module ActiveRecord sql.should_not match /ORDER BY/ end + it 'reverses the order if used with last' do + expected = Person.all.sort_by { |p| p.id }.last + result = Person.reorder(:id).last + result.should eq(expected) + end end describe '#from' do