1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #10898 from dmitry/find_first_refactor_duplication

Refactored ActiveRecord `first` method to get rid of duplication.
This commit is contained in:
Rafael Mendonça França 2013-06-14 13:17:42 -07:00
commit f7114754b1

View file

@ -124,11 +124,7 @@ module ActiveRecord
# #
def first(limit = nil) def first(limit = nil)
if limit if limit
if order_values.empty? && primary_key find_first_with_limit(order_values, limit)
order(arel_table[primary_key].asc).limit(limit).to_a
else
limit(limit).to_a
end
else else
find_first find_first
end end
@ -358,12 +354,15 @@ module ActiveRecord
if loaded? if loaded?
@records.first @records.first
else else
@first ||= @first ||= find_first_with_limit(with_default_scope.order_values, 1).first
if with_default_scope.order_values.empty? && primary_key end
order(arel_table[primary_key].asc).limit(1).to_a.first end
else
limit(1).to_a.first def find_first_with_limit(order_values, limit)
end if order_values.empty? && primary_key
order(arel_table[primary_key].asc).limit(limit).to_a
else
limit(limit).to_a
end end
end end