From 111968d4024fdccc386979551cdfc7799b39cff0 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sat, 9 Jul 2011 11:37:48 +0100 Subject: Foo.joins(:bar).includes(:bar) should result in a single query with :bar as a join. Related: #1873. --- activerecord/lib/active_record/relation.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/relation.rb') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 6258a16d0f..2d0861d5c9 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -408,7 +408,17 @@ module ActiveRecord end def eager_loading? - @should_eager_load ||= (@eager_load_values.any? || (@includes_values.any? && references_eager_loaded_tables?)) + @should_eager_load ||= + @eager_load_values.any? || + @includes_values.any? && (joined_includes_values.any? || references_eager_loaded_tables?) + end + + # Joins that are also marked for preloading. In which case we should just eager load them. + # Note that this is a naive implementation because we could have strings and symbols which + # represent the same association, but that aren't matched by this. Also, we could have + # nested hashes which partially match, e.g. { :a => :b } & { :a => [:b, :c] } + def joined_includes_values + @includes_values & @joins_values end def ==(other) -- cgit v1.2.3