aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamien Mathieu <42@dmathieu.com>2011-09-01 11:24:19 +0200
committerDamien Mathieu <42@dmathieu.com>2011-09-05 13:09:52 +0200
commit451cdd62ce54080d0a4944551fbc3f10cc34a42d (patch)
treefd5c5b14c76a5aab88ee7081bcb363134b9e9cd4
parent24ee573d6ce8bb11f7eb62224feca5a87fdd2a69 (diff)
downloadrails-451cdd62ce54080d0a4944551fbc3f10cc34a42d.tar.gz
rails-451cdd62ce54080d0a4944551fbc3f10cc34a42d.tar.bz2
rails-451cdd62ce54080d0a4944551fbc3f10cc34a42d.zip
use LIMIT SQL word in first - Closes #2783
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/finder_test.rb9
2 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 73368aed18..2639c46709 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
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 5dc5f99582..fbcae27719 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -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