aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorLann Martin <lann@causes.com>2013-09-04 16:47:28 -0700
committerLann Martin <lann@lannbox.com>2013-09-09 15:17:44 -0600
commit8875e28a50b117aa862c8563c49f7e3a6ee7deff (patch)
treefe508015d39bc6ca217976ab3d814a6517f2c926 /activerecord/lib
parentc27fde26166f71ec68a7fb501435b656f436a687 (diff)
downloadrails-8875e28a50b117aa862c8563c49f7e3a6ee7deff.tar.gz
rails-8875e28a50b117aa862c8563c49f7e3a6ee7deff.tar.bz2
rails-8875e28a50b117aa862c8563c49f7e3a6ee7deff.zip
Make CollectionAssociation first/last with integer fetch with query
When first or last is called with an integer on an unloaded association, the entire collection is loaded. This differs surprisingly from the behavior of Relation#first/last, which translate the call into a limit query. For large collections this can make a big difference in performance. Change CollectionAssociation#fetch_first_or_last_using_find? to make this kind of call delegate to Relation.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb4
1 files changed, 1 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 228c500f0a..0c326ed747 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -528,15 +528,13 @@ module ActiveRecord
# * target already loaded
# * owner is new record
# * target contains new or changed record(s)
- # * the first arg is an integer (which indicates the number of records to be returned)
def fetch_first_or_last_using_find?(args)
if args.first.is_a?(Hash)
true
else
!(loaded? ||
owner.new_record? ||
- target.any? { |record| record.new_record? || record.changed? } ||
- args.first.kind_of?(Integer))
+ target.any? { |record| record.new_record? || record.changed? })
end
end