From 592e6b7e894e0fff07ab8233fce4bf77968ee8b6 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Thu, 18 Aug 2016 06:29:40 +0900 Subject: Remove unnecessary ordinal methods for collection association Currently `CollectionProxy` inherits `Relation` therefore we can use its own methods rather than delegating to collection association. --- activerecord/lib/active_record/relation/finder_methods.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/relation/finder_methods.rb') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index ff43def901..376867675b 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -97,7 +97,7 @@ module ActiveRecord # Person.take(5) # returns 5 objects fetched by SELECT * FROM people LIMIT 5 # Person.where(["name LIKE '%?'", name]).take def take(limit = nil) - limit ? limit(limit).to_a : find_take + limit ? find_take_with_limit(limit) : find_take end # Same as #take but raises ActiveRecord::RecordNotFound if no record @@ -526,13 +526,21 @@ module ActiveRecord end end + def find_take_with_limit(limit) + if loaded? + records.take(limit) + else + limit(limit).to_a + end + end + def find_nth(index) @offsets[offset_index + index] ||= find_nth_with_limit(index, 1).first end def find_nth_with_limit(index, limit) if loaded? - records[index, limit] + records[index, limit] || [] else relation = if order_values.empty? && primary_key order(arel_attribute(primary_key).asc) -- cgit v1.2.3