use LIMIT SQL word in first - Closes #2783

This commit is contained in:
Damien Mathieu
2011-09-01 11:24:19 +02:00
parent 24ee573d6c
commit 451cdd62ce
2 changed files with 10 additions and 1 deletions

View File

@@ -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

View File

@@ -243,6 +243,15 @@ class FinderTest < ActiveRecord::TestCase
end
end
def test_first_with_integer_should_use_sql_limit
assert_sql(/LIMIT 2/) { Topic.first(2).entries }
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