From ed15217b054de379d7b0d283d08b71f185fdf2e9 Mon Sep 17 00:00:00 2001
From: Ben Woosley <ben.woosley@gmail.com>
Date: Wed, 27 Jan 2016 21:14:31 -0800
Subject: Consistently warn that passing an offset to `find_nth` is deprecated

@bogdan pointed out that a `loaded?` relation would not warn that the supplied
offset would be removed. This fixes that oversight.

https://github.com/rails/rails/commit/16a476e4f8f802774ae7c8dca2e59f4e672dc591#commitcomment-15706834

Although this second argument is probably not widely used, and would be
ignored anyway in the loaded? case, this could protect callers from gotchas.

[Ben Woosley & Victor Kmita]
---
 .../lib/active_record/relation/finder_methods.rb    | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

(limited to 'activerecord')

diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 3cbb12a09d..3f5d6de78a 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -489,20 +489,19 @@ module ActiveRecord
     end
 
     def find_nth(index, offset = nil)
+      # TODO: once the offset argument is removed we rely on offset_index
+      # within find_nth_with_limit, rather than pass it in via
+      # find_nth_with_limit_and_offset
+      if offset
+        ActiveSupport::Deprecation.warn(<<-MSG.squish)
+          Passing an offset argument to find_nth is deprecated,
+          please use Relation#offset instead.
+        MSG
+      end
       if loaded?
         @records[index]
       else
-        # TODO: once the offset argument is removed we rely on offset_index
-        # within find_nth_with_limit, rather than pass it in via
-        # find_nth_with_limit_and_offset
-        if offset
-          ActiveSupport::Deprecation.warn(<<-MSG.squish)
-            Passing an offset argument to find_nth is deprecated,
-            please use Relation#offset instead.
-          MSG
-        else
-          offset = offset_index
-        end
+        offset ||= offset_index
         @offsets[offset + index] ||= find_nth_with_limit_and_offset(index, 1, offset: offset).first
       end
     end
-- 
cgit v1.2.3