aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-04-12 09:45:39 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2014-04-12 09:45:39 -0700
commit8c2b79d0c8f3533f8045baf647317c4ff33b0f26 (patch)
treeccbf19645156996d88eb37d5aad0be69a4043f7c /activerecord
parent0405d5a7e95776f9adf5b8ff064300898d89b43a (diff)
downloadrails-8c2b79d0c8f3533f8045baf647317c4ff33b0f26.tar.gz
rails-8c2b79d0c8f3533f8045baf647317c4ff33b0f26.tar.bz2
rails-8c2b79d0c8f3533f8045baf647317c4ff33b0f26.zip
only add the offset and index when we need to
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb17
1 files changed, 9 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 6274ca15ca..8daea2c13e 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -131,7 +131,7 @@ module ActiveRecord
if limit
find_nth_with_limit(offset_value, limit)
else
- find_nth(:first, offset_index)
+ find_nth(0, offset_index)
end
end
@@ -181,7 +181,7 @@ module ActiveRecord
# Person.offset(3).second # returns the second object from OFFSET 3 (which is OFFSET 4)
# Person.where(["user_name = :u", { u: user_name }]).second
def second
- find_nth(:second, offset_index + 1)
+ find_nth(1, offset_index)
end
# Same as +second+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -197,7 +197,7 @@ module ActiveRecord
# Person.offset(3).third # returns the third object from OFFSET 3 (which is OFFSET 5)
# Person.where(["user_name = :u", { u: user_name }]).third
def third
- find_nth(:third, offset_index + 2)
+ find_nth(2, offset_index)
end
# Same as +third+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -213,7 +213,7 @@ module ActiveRecord
# Person.offset(3).fourth # returns the fourth object from OFFSET 3 (which is OFFSET 6)
# Person.where(["user_name = :u", { u: user_name }]).fourth
def fourth
- find_nth(:fourth, offset_index + 3)
+ find_nth(3, offset_index)
end
# Same as +fourth+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -229,7 +229,7 @@ module ActiveRecord
# Person.offset(3).fifth # returns the fifth object from OFFSET 3 (which is OFFSET 7)
# Person.where(["user_name = :u", { u: user_name }]).fifth
def fifth
- find_nth(:fifth, offset_index + 4)
+ find_nth(4, offset_index)
end
# Same as +fifth+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -245,7 +245,7 @@ module ActiveRecord
# Person.offset(3).forty_two # returns the fifth object from OFFSET 3 (which is OFFSET 44)
# Person.where(["user_name = :u", { u: user_name }]).forty_two
def forty_two
- find_nth(:forty_two, offset_index + 41)
+ find_nth(41, offset_index)
end
# Same as +forty_two+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -472,10 +472,11 @@ module ActiveRecord
end
end
- def find_nth(ordinal, offset)
+ def find_nth(index, offset)
if loaded?
- @records.send(ordinal)
+ @records[index]
else
+ offset += index
@offsets[offset] ||= find_nth_with_limit(offset, 1).first
end
end