From 7ea1da65d7e14e420cdb5957618db37f092badda Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Sat, 27 Feb 2016 11:39:02 -0800 Subject: refactor AR second_to_last to use array methods --- activerecord/lib/active_record/relation/finder_methods.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/relation/finder_methods.rb') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 245976dc2b..960c409d4b 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -565,7 +565,12 @@ module ActiveRecord if loaded? @records[-index] else - reverse_order.offset(index-1).first + 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), + # or by using a combination of reverse_order, limit, and offset, + # e.g., reverse_order.offset(index-1).first end end -- cgit v1.2.3