From c17fadaa7f21afce8d9ffd6acaabf1f9b06aa5b8 Mon Sep 17 00:00:00 2001 From: Jake Yesbeck Date: Thu, 14 Apr 2016 17:50:22 -0700 Subject: [PATCH] Fix Reported issue involving reoder and last This should Resolve #418 Since the 4.1 version of `reverse_order!` is used, an additional check has been added to actually reorder the `ORDER BY` clause if there are non-nil elements of it. Additionally, a spec has been added to ensure proper ordering takes place. --- lib/squeel/adapters/active_record/4.1/relation_extensions.rb | 4 +++- .../adapters/active_record/relation_extensions_spec.rb | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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