aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorBrian Christian <brchristian@gmail.com>2016-02-27 11:52:52 -0800
committerBrian Christian <brchristian@gmail.com>2016-02-27 11:52:52 -0800
commitf9ddfc3b4b2ab6eebaa4ddb33347e715e3a82375 (patch)
treeba50cf7a304205d88a1de308f408203d18d6809c /activerecord
parent7ea1da65d7e14e420cdb5957618db37f092badda (diff)
downloadrails-f9ddfc3b4b2ab6eebaa4ddb33347e715e3a82375.tar.gz
rails-f9ddfc3b4b2ab6eebaa4ddb33347e715e3a82375.tar.bz2
rails-f9ddfc3b4b2ab6eebaa4ddb33347e715e3a82375.zip
default second_to_last to primary_key index if no order supplied
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb8
1 files changed, 7 insertions, 1 deletions
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),