mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
use LIMIT SQL word in first - Closes #2783
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user