diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2016-06-04 18:39:18 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-02-26 21:20:39 +0900 |
commit | 84f4ab90dabbe23db0cd6f74078da6d341401d10 (patch) | |
tree | dbc6ee8a13c2206f22741227e802ff249d151f6a | |
parent | 228c6c07f1b5e786c72d9e30b2755d764f7a425d (diff) | |
download | rails-84f4ab90dabbe23db0cd6f74078da6d341401d10.tar.gz rails-84f4ab90dabbe23db0cd6f74078da6d341401d10.tar.bz2 rails-84f4ab90dabbe23db0cd6f74078da6d341401d10.zip |
Fix `find_nth` with `limit_value`
If the `index` exceeds a `limit`, simply return an empty result without
querying the database.
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 6 |
2 files changed, 10 insertions, 6 deletions
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 diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index deec669935..89d8a8bdca 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -497,7 +497,7 @@ class FinderTest < ActiveRecord::TestCase assert_nil Topic.offset(5).second_to_last #test with limit - # assert_nil Topic.limit(1).second # TODO: currently failing + assert_nil Topic.limit(1).second assert_nil Topic.limit(1).second_to_last end @@ -526,9 +526,9 @@ class FinderTest < ActiveRecord::TestCase assert_nil Topic.offset(5).third_to_last # test with limit - # assert_nil Topic.limit(1).third # TODO: currently failing + assert_nil Topic.limit(1).third assert_nil Topic.limit(1).third_to_last - # assert_nil Topic.limit(2).third # TODO: currently failing + assert_nil Topic.limit(2).third assert_nil Topic.limit(2).third_to_last end |