From 2b73f780ffa52baba09511b2db753f0fde574c14 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sat, 11 May 2013 00:34:25 -0400 Subject: do not load all child records for inverse case currently `post.comments.find(Comment.first.id)` would load all comments for the given post to set the inverse association. This has a huge performance penalty. Because if post has 100k records and all these 100k records would be loaded in memory even though the comment id was supplied. Fix is to use in-memory records only if loaded? is true. Otherwise load the records using full sql. Fixes #10509 --- activerecord/lib/active_record/associations/collection_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index efd7ecb97c..9833822f8f 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -81,7 +81,7 @@ module ActiveRecord else if options[:finder_sql] find_by_scan(*args) - elsif options[:inverse_of] + elsif options[:inverse_of] && loaded? args = args.flatten raise RecordNotFound, "Couldn't find #{scope.klass.name} without an ID" if args.blank? -- cgit v1.2.3