提交 f7114754 编写于 作者: R Rafael Mendonça França

Merge pull request #10898 from dmitry/find_first_refactor_duplication

Refactored ActiveRecord `first` method to get rid of duplication.
......@@ -124,11 +124,7 @@ def take!
#
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 @@ def find_first
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册