From f9ddfc3b4b2ab6eebaa4ddb33347e715e3a82375 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Sat, 27 Feb 2016 11:52:52 -0800 Subject: default second_to_last to primary_key index if no order supplied --- activerecord/lib/active_record/relation/finder_methods.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 960c409d4b..c3053f0b13 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -565,7 +565,13 @@ module ActiveRecord if loaded? @records[-index] else - to_a[-index] + relation = if order_values.empty? && primary_key + order(arel_attribute(primary_key).asc) + else + self + end + + relation.to_a[-index] # TODO: can be made more performant on large result sets by # for instance, last(index)[-index] (which would require # refactoring the last(n) finder method to make test suite pass), -- cgit v1.2.3