diff options
author | Dmitry Polushkin <dmitry.polushkin@gmail.com> | 2013-06-09 19:36:39 +0200 |
---|---|---|
committer | Dmitry Polushkin <dmitry.polushkin@gmail.com> | 2013-06-09 19:36:39 +0200 |
commit | d5450a6659a33ef08a170afcaae084023bcd275b (patch) | |
tree | 1397bca75489ac9fb946f9479d4572f703b4f3b9 | |
parent | 7a32c6300c2b11dc1660338535c653a0132df196 (diff) | |
download | rails-d5450a6659a33ef08a170afcaae084023bcd275b.tar.gz rails-d5450a6659a33ef08a170afcaae084023bcd275b.tar.bz2 rails-d5450a6659a33ef08a170afcaae084023bcd275b.zip |
Refactored ActiveRecord `first` method to get rid of duplication.
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 7ddaea1bb0..cf71df8fba 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -81,11 +81,7 @@ module ActiveRecord # Person.first(3) # returns the first three objects fetched by SELECT * FROM people LIMIT 3 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_records(order_values, limit) else find_first end @@ -307,12 +303,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_records(with_default_scope.order_values, 1).first + end + end + + def find_first_records(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 |