diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-03-07 00:04:06 +0000 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-03-07 08:51:17 -0800 |
commit | 532f915037ab65873c433e30399d11f93df9f1f8 (patch) | |
tree | 2d177e4d59e9ea66a3d0ac8ebf168967446cd64a /activerecord | |
parent | 9cee693213e0205cb5992728bb516215e34cb79f (diff) | |
download | rails-532f915037ab65873c433e30399d11f93df9f1f8.tar.gz rails-532f915037ab65873c433e30399d11f93df9f1f8.tar.bz2 rails-532f915037ab65873c433e30399d11f93df9f1f8.zip |
Referencing a table via the ON condition in a join should force that table to be eager-loaded via a JOIN rather than via subsequent queries.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 15 |
2 files changed, 27 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index f939bedc81..5af20bf38b 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -407,8 +407,19 @@ module ActiveRecord private def references_eager_loaded_tables? + joined_tables = arel.join_sources.map do |join| + if join.is_a?(Arel::Nodes::StringJoin) + tables_in_string(join.left) + else + [join.left.table_name, join.left.table_alias] + end + end + + joined_tables += [table.name, table.table_alias] + # always convert table names to downcase as in Oracle quoted table names are in uppercase - joined_tables = (tables_in_string(arel.join_sql) + [table.name, table.table_alias]).compact.map{ |t| t.downcase }.uniq + joined_tables = joined_tables.flatten.compact.map { |t| t.downcase }.uniq + (tables_in_string(to_sql) - joined_tables).any? end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 37bbb17e74..948fcf7012 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -850,4 +850,19 @@ class RelationTest < ActiveRecord::TestCase def test_primary_key assert_equal "id", Post.scoped.primary_key end + + def test_eager_loading_with_conditions_on_joins + scope = Post.includes(:comments) + + # This references the comments table, and so it should cause the comments to be eager + # loaded via a JOIN, rather than by subsequent queries. + scope = scope.joins( + Post.arel_table.create_join( + Post.arel_table, + Post.arel_table.create_on(Comment.arel_table[:id].eq(3)) + ) + ) + + assert scope.eager_loading? + end end |