aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-09-05 05:41:46 -0700
committerJon Leighton <j@jonathanleighton.com>2011-09-05 05:41:46 -0700
commite221108c141fa564b7da6378478b762b356e0084 (patch)
tree9988d58da2e27c67a6de9e01137f81b3dc9a6c9b /activerecord/lib/active_record
parent24ee573d6ce8bb11f7eb62224feca5a87fdd2a69 (diff)
parent5f5527c726841cdefb82965a645d554767c5a6a9 (diff)
downloadrails-e221108c141fa564b7da6378478b762b356e0084.tar.gz
rails-e221108c141fa564b7da6378478b762b356e0084.tar.bz2
rails-e221108c141fa564b7da6378478b762b356e0084.zip
Merge pull request #2789 from dmathieu/limit_first_last
Use LIMIT SQL word in first - Closes #2783
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb8
1 files changed, 6 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 73368aed18..83d650d3f4 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -114,7 +114,7 @@ module ActiveRecord
def first(*args)
if args.any?
if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash))
- to_a.first(*args)
+ limit(*args).to_a
else
apply_finder_options(args.first).first
end
@@ -134,7 +134,11 @@ module ActiveRecord
def last(*args)
if args.any?
if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash))
- to_a.last(*args)
+ if order_values.empty? && reorder_value.nil?
+ order("#{primary_key} DESC").limit(*args).reverse
+ else
+ to_a.last(*args)
+ end
else
apply_finder_options(args.first).last
end