Permalink
Browse files

Make referencing an included item trigger eager loading

  • Loading branch information...
1 parent 4c4760a commit 4429f16c0ada896fe3753324f0201b290c6425d9 @jonleighton jonleighton committed Jan 14, 2012
Showing with 13 additions and 1 deletion.
  1. +2 −1 activerecord/lib/active_record/relation.rb
  2. +11 −0 activerecord/test/cases/relations_test.rb
@@ -521,7 +521,8 @@ def references_eager_loaded_tables?
# always convert table names to downcase as in Oracle quoted table names are in uppercase
joined_tables = joined_tables.flatten.compact.map { |t| t.downcase }.uniq
- (tables_in_string(to_sql) - joined_tables).any?
+ (tables_in_string(to_sql) - joined_tables).any? ||
+ (includes_values & references_values).any?
end
def tables_in_string(string)
@@ -1164,4 +1164,15 @@ def test_uniq
end
assert_equal ['Foo', 'Foo'], query.uniq(true).uniq(false).map(&:name)
end
+
+ def test_references_triggers_eager_loading
+ scope = Post.includes(:comments)
+ assert !scope.eager_loading?
+ assert scope.references(:comments).eager_loading?
+ end
+
+ def test_references_doesnt_trigger_eager_loading_if_reference_not_included
+ scope = Post.references(:comments)
+ assert !scope.eager_loading?
+ end
end

0 comments on commit 4429f16

Please sign in to comment.