diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-09-05 05:41:46 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-09-05 05:41:46 -0700 |
commit | e221108c141fa564b7da6378478b762b356e0084 (patch) | |
tree | 9988d58da2e27c67a6de9e01137f81b3dc9a6c9b | |
parent | 24ee573d6ce8bb11f7eb62224feca5a87fdd2a69 (diff) | |
parent | 5f5527c726841cdefb82965a645d554767c5a6a9 (diff) | |
download | rails-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
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 26 |
2 files changed, 32 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 diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 5dc5f99582..d840d38678 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -243,6 +243,32 @@ class FinderTest < ActiveRecord::TestCase end end + def test_first_and_last_with_integer_should_use_sql_limit + assert_sql(/LIMIT 2/) { Topic.first(2).entries } + assert_sql(/LIMIT 5/) { Topic.last(5).entries } + end + + def test_last_with_integer_and_order_should_keep_the_order + assert_equal Topic.order("title").to_a.last(2), Topic.order("title").last(2) + end + + def test_last_with_integer_and_order_should_not_use_sql_limit + query = assert_sql { Topic.order("title").last(5).entries } + assert_equal 1, query.length + assert_no_match(/LIMIT/, query.first) + end + + def test_last_with_integer_and_reorder_should_not_use_sql_limit + query = assert_sql { Topic.reorder("title").last(5).entries } + assert_equal 1, query.length + assert_no_match(/LIMIT/, query.first) + end + + def test_first_and_last_with_integer_should_return_an_array + assert_kind_of Array, Topic.first(5) + assert_kind_of Array, Topic.last(5) + end + def test_unexisting_record_exception_handling assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1).parent |