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:
commit
f7114754b1
1 changed files with 10 additions and 11 deletions
|
@ -124,11 +124,7 @@ module ActiveRecord
|
|||
#
|
||||
def first(limit = nil)
|
||||
if limit
|
||||
if order_values.empty? && primary_key
|
||||
order(arel_table[primary_key].asc).limit(limit).to_a
|
||||
else
|
||||
limit(limit).to_a
|
||||
end
|
||||
find_first_with_limit(order_values, limit)
|
||||
else
|
||||
find_first
|
||||
end
|
||||
|
@ -358,12 +354,15 @@ module ActiveRecord
|
|||
if loaded?
|
||||
@records.first
|
||||
else
|
||||
@first ||=
|
||||
if with_default_scope.order_values.empty? && primary_key
|
||||
order(arel_table[primary_key].asc).limit(1).to_a.first
|
||||
else
|
||||
limit(1).to_a.first
|
||||
end
|
||||
@first ||= find_first_with_limit(with_default_scope.order_values, 1).first
|
||||
end
|
||||
end
|
||||
|
||||
def find_first_with_limit(order_values, limit)
|
||||
if order_values.empty? && primary_key
|
||||
order(arel_table[primary_key].asc).limit(limit).to_a
|
||||
else
|
||||
limit(limit).to_a
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue