diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-06-27 14:29:35 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-27 14:29:35 -0400 |
commit | 7f9986237f45a9e771562470bbba629881d0f4aa (patch) | |
tree | 7adcff8cd5bcff98d812a7437edde453be0b9dac /activerecord/lib | |
parent | faa225fd029180cb06287be36fad6c742e8e8261 (diff) | |
parent | f250da5397b967fcba58356547bc26127c1be93e (diff) | |
download | rails-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.rb | 23 |
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 |