Browse files

don't bother with an offset if the offset is zero

we're guaranteed to pass a numeric value for offset, so if it's zero,
just don't add an offset to the query
  • Loading branch information...
1 parent 8c2b79d commit 711a882f106385e248135c84ea9a6a827f0bd1e1 @tenderlove tenderlove committed Apr 12, 2014
Showing with 9 additions and 6 deletions.
  1. +9 −6 activerecord/lib/active_record/relation/finder_methods.rb
View
15 activerecord/lib/active_record/relation/finder_methods.rb
@@ -129,7 +129,7 @@ def take!
#
def first(limit = nil)
if limit
- find_nth_with_limit(offset_value, limit)
+ find_nth_with_limit(offset_index, limit)
else
find_nth(0, offset_index)
end
@@ -482,11 +482,14 @@ def find_nth(index, offset)
end
def find_nth_with_limit(offset, limit)
- if order_values.empty? && primary_key
- order(arel_table[primary_key].asc).limit(limit).offset(offset).to_a
- else
- limit(limit).offset(offset).to_a
- end
+ relation = if order_values.empty? && primary_key
+ order(arel_table[primary_key].asc)
+ else
+ self
+ end
+
+ relation = relation.offset(offset) unless offset.zero?
+ relation.limit(limit).to_a
end
def find_last

0 comments on commit 711a882

Please sign in to comment.