aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-06-27 14:29:35 -0400
committerGitHub <noreply@github.com>2017-06-27 14:29:35 -0400
commit7f9986237f45a9e771562470bbba629881d0f4aa (patch)
tree7adcff8cd5bcff98d812a7437edde453be0b9dac /activerecord/lib
parentfaa225fd029180cb06287be36fad6c742e8e8261 (diff)
parentf250da5397b967fcba58356547bc26127c1be93e (diff)
downloadrails-7f9986237f45a9e771562470bbba629881d0f4aa.tar.gz
rails-7f9986237f45a9e771562470bbba629881d0f4aa.tar.bz2
rails-7f9986237f45a9e771562470bbba629881d0f4aa.zip
Merge pull request #29556 from kamipo/extract_ordered_relation
Extract `ordered_relation` in `FinderMethods`
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb23
1 files changed, 11 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 1d661fa8ed..5ec2dfcfc9 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -147,8 +147,7 @@ module ActiveRecord
def last(limit = nil)
return find_last(limit) if loaded? || limit_value
- result = limit(limit)
- result.order!(arel_attribute(primary_key)) if order_values.empty? && primary_key
+ result = ordered_relation.limit(limit)
result = result.reverse_order!
limit ? result.reverse : result.first
@@ -535,11 +534,7 @@ module ActiveRecord
if loaded?
records[index, limit] || []
else
- relation = if order_values.empty? && primary_key
- order(arel_attribute(primary_key).asc)
- else
- self
- end
+ relation = ordered_relation
if limit_value.nil? || index < limit_value
relation = relation.offset(offset_index + index) unless index.zero?
@@ -554,11 +549,7 @@ module ActiveRecord
if loaded?
records[-index]
else
- relation = if order_values.empty? && primary_key
- order(arel_attribute(primary_key).asc)
- else
- self
- end
+ relation = ordered_relation
relation.to_a[-index]
# TODO: can be made more performant on large result sets by
@@ -572,5 +563,13 @@ module ActiveRecord
def find_last(limit)
limit ? records.last(limit) : records.last
end
+
+ def ordered_relation
+ if order_values.empty? && primary_key
+ order(arel_attribute(primary_key).asc)
+ else
+ self
+ end
+ end
end
end