diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-01-14 00:17:39 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-01-16 21:17:17 +0000 |
commit | 4429f16c0ada896fe3753324f0201b290c6425d9 (patch) | |
tree | db8234ec210f74346f40b4b0c1e200add3e33f66 | |
parent | 4c4760a619e9bddf14e65dd7f0d5bbc3f9ca320e (diff) | |
download | rails-4429f16c0ada896fe3753324f0201b290c6425d9.tar.gz rails-4429f16c0ada896fe3753324f0201b290c6425d9.tar.bz2 rails-4429f16c0ada896fe3753324f0201b290c6425d9.zip |
Make referencing an included item trigger eager loading
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 11 |
2 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 0f8ac33f6e..dcfce448b1 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -521,7 +521,8 @@ module ActiveRecord # 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) diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index bf1eb6386a..c089ed164c 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1164,4 +1164,15 @@ class RelationTest < ActiveRecord::TestCase 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 |