diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2016-08-18 06:29:40 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2016-08-18 07:35:16 +0900 |
commit | 592e6b7e894e0fff07ab8233fce4bf77968ee8b6 (patch) | |
tree | 7954bd08b50fdabfa67e3e94061b7aeea295c576 /activerecord/lib/active_record/relation/finder_methods.rb | |
parent | 6568cfd78c89fe70ac7304d03f8f4825fe0b7c72 (diff) | |
download | rails-592e6b7e894e0fff07ab8233fce4bf77968ee8b6.tar.gz rails-592e6b7e894e0fff07ab8233fce4bf77968ee8b6.tar.bz2 rails-592e6b7e894e0fff07ab8233fce4bf77968ee8b6.zip |
Remove unnecessary ordinal methods for collection association
Currently `CollectionProxy` inherits `Relation` therefore we can use
its own methods rather than delegating to collection association.
Diffstat (limited to 'activerecord/lib/active_record/relation/finder_methods.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 12 |
1 files changed, 10 insertions, 2 deletions
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) |