From 84f4ab90dabbe23db0cd6f74078da6d341401d10 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sat, 4 Jun 2016 18:39:18 +0900 Subject: Fix `find_nth` with `limit_value` If the `index` exceeds a `limit`, simply return an empty result without querying the database. --- activerecord/lib/active_record/relation/finder_methods.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 6412c7b22e..5d24f5f5ca 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -147,7 +147,7 @@ module ActiveRecord def last(limit = nil) return find_last(limit) if loaded? || limit_value - result = limit(limit || 1) + result = limit(limit) result.order!(arel_attribute(primary_key)) if order_values.empty? && primary_key result = result.reverse_order! @@ -536,8 +536,12 @@ module ActiveRecord self end - relation = relation.offset(offset_index + index) unless index.zero? - relation.limit(limit).to_a + if limit_value.nil? || index < limit_value + relation = relation.offset(offset_index + index) unless index.zero? + relation.limit(limit).to_a + else + [] + end end end -- cgit v1.2.3