From 7aaf4867d2d7c1adea419052f069dab542af13b9 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 16 Aug 2006 18:10:52 +0000 Subject: Included associations: go deep. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4776 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 22 ++++++-------- .../associations_cascaded_eager_loading_test.rb | 35 +++++++++++++++++++--- .../test/fixtures/db_definitions/schema.rb | 10 +++++++ activerecord/test/fixtures/edge.rb | 5 ++++ activerecord/test/fixtures/edges.yml | 6 ++++ activerecord/test/fixtures/mixin.rb | 1 + activerecord/test/fixtures/vertex.rb | 9 ++++++ activerecord/test/fixtures/vertices.yml | 4 +++ 8 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 activerecord/test/fixtures/edge.rb create mode 100644 activerecord/test/fixtures/edges.yml create mode 100644 activerecord/test/fixtures/vertex.rb create mode 100644 activerecord/test/fixtures/vertices.yml (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 4f2d8a7999..8f71734b3d 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1393,9 +1393,9 @@ module ActiveRecord unless join_dependency.table_aliases[aliased_table_name].zero? # if the table name has been used, then use an alias - @aliased_table_name = cascade_alias + @aliased_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}" table_index = join_dependency.table_aliases[aliased_table_name] - join_dependency.table_aliases[@aliased_table_name] += 1 + join_dependency.table_aliases[aliased_table_name] += 1 @aliased_table_name = @aliased_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0 else join_dependency.table_aliases[aliased_table_name] += 1 @@ -1406,9 +1406,11 @@ module ActiveRecord unless join_dependency.table_aliases[aliased_join_table_name].zero? @aliased_join_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}_join" table_index = join_dependency.table_aliases[aliased_join_table_name] + join_dependency.table_aliases[aliased_join_table_name] += 1 @aliased_join_table_name = @aliased_join_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0 + else + join_dependency.table_aliases[aliased_join_table_name] += 1 end - join_dependency.table_aliases[aliased_join_table_name] += 1 end end @@ -1419,7 +1421,7 @@ module ActiveRecord table_alias_for(options[:join_table], aliased_join_table_name), aliased_join_table_name, options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key, - reflection.active_record.table_name, reflection.active_record.primary_key] + + parent.aliased_table_name, reflection.active_record.primary_key] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_name_and_alias, aliased_table_name, klass.primary_key, aliased_join_table_name, options[:association_foreign_key] || klass.table_name.classify.foreign_key @@ -1457,15 +1459,14 @@ module ActiveRecord case source_reflection.macro when :belongs_to first_key = primary_key - second_key = options[:foreign_key] || klass.to_s.classify.foreign_key + second_key = source_reflection.options[:foreign_key] || klass.to_s.classify.foreign_key when :has_many first_key = through_reflection.klass.to_s.classify.foreign_key second_key = options[:foreign_key] || primary_key end - " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ - table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, - through_reflection.primary_key_name, + table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), + aliased_join_table_name, through_reflection.primary_key_name, parent.aliased_table_name, parent.primary_key] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_name_and_alias, @@ -1531,11 +1532,6 @@ module ActiveRecord def interpolate_sql(sql) instance_eval("%@#{sql.gsub('@', '\@')}@") end - - private - def cascade_alias - active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}" - end end end end diff --git a/activerecord/test/associations_cascaded_eager_loading_test.rb b/activerecord/test/associations_cascaded_eager_loading_test.rb index 20df8cd30e..863af199ce 100644 --- a/activerecord/test/associations_cascaded_eager_loading_test.rb +++ b/activerecord/test/associations_cascaded_eager_loading_test.rb @@ -11,7 +11,7 @@ require 'fixtures/topic' require 'fixtures/reply' class CascadedEagerLoadingTest < Test::Unit::TestCase - fixtures :authors, :mixins, :companies, :posts, :categorizations, :topics + fixtures :authors, :mixins, :companies, :posts, :topics def test_eager_association_loading_with_cascaded_two_levels authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id") @@ -94,7 +94,7 @@ class CascadedEagerLoadingTest < Test::Unit::TestCase author.posts.first.very_special_comment end end - + def test_eager_association_loading_of_stis_with_multiple_references authors = Author.find(:all, :include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4') assert_equal [authors(:david)], authors @@ -103,9 +103,36 @@ class CascadedEagerLoadingTest < Test::Unit::TestCase authors.first.posts.first.special_comments.first.post.very_special_comment end end - - def test_eager_association_loading_with_recursive_cascaded_three_levels + + def test_eager_association_loading_with_recursive_cascading_three_levels_has_many root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:children=>{:children=>:children}}, :order => 'mixins.id') assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.children.first.children.first.children.first } end + + def test_eager_association_loading_with_recursive_cascading_three_levels_has_one + root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:first_child=>{:first_child=>:first_child}}, :order => 'mixins.id') + assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.first_child.first_child.first_child } + end + + def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to + leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:parent=>{:parent=>:parent}}, :order => 'mixins.id DESC') + assert_equal mixins(:recursively_cascaded_tree_1), assert_no_queries { leaf_node.parent.parent.parent } + end +end + + +require 'fixtures/vertex' +require 'fixtures/edge' +class CascadedEagerLoadingTest < Test::Unit::TestCase + fixtures :edges, :vertices + + def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through + source = Vertex.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id') + assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first } + end + + def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many + sink = Vertex.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC') + assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first } + end end diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index a5f2c9dc10..f565b74564 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -39,4 +39,14 @@ ActiveRecord::Schema.define do t.column :author_id, :integer t.column :favorite_author_id, :integer end + + create_table :vertices, :force => true do |t| + t.column :label, :string + end + + create_table :edges, :force => true do |t| + t.column :source_id, :integer, :null => false + t.column :sink_id, :integer, :null => false + end + add_index :edges, [:source_id, :sink_id], :unique => true end diff --git a/activerecord/test/fixtures/edge.rb b/activerecord/test/fixtures/edge.rb new file mode 100644 index 0000000000..55e0c31fcb --- /dev/null +++ b/activerecord/test/fixtures/edge.rb @@ -0,0 +1,5 @@ +# This class models an edge in a directed graph. +class Edge < ActiveRecord::Base + belongs_to :source, :class_name => 'Vertex', :foreign_key => 'source_id' + belongs_to :sink, :class_name => 'Vertex', :foreign_key => 'sink_id' +end diff --git a/activerecord/test/fixtures/edges.yml b/activerecord/test/fixtures/edges.yml new file mode 100644 index 0000000000..c16c70dd2f --- /dev/null +++ b/activerecord/test/fixtures/edges.yml @@ -0,0 +1,6 @@ +<% (1..4).each do |id| %> +edge_<%= id %>: + id: <%= id %> + source_id: <%= id %> + sink_id: <%= id + 1 %> +<% end %> \ No newline at end of file diff --git a/activerecord/test/fixtures/mixin.rb b/activerecord/test/fixtures/mixin.rb index afc976f8f1..1f200da62d 100644 --- a/activerecord/test/fixtures/mixin.rb +++ b/activerecord/test/fixtures/mixin.rb @@ -12,6 +12,7 @@ end class RecursivelyCascadedTreeMixin < Mixin acts_as_tree :foreign_key => "parent_id" + has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id end class ListMixin < Mixin diff --git a/activerecord/test/fixtures/vertex.rb b/activerecord/test/fixtures/vertex.rb new file mode 100644 index 0000000000..f4c11144de --- /dev/null +++ b/activerecord/test/fixtures/vertex.rb @@ -0,0 +1,9 @@ +# This class models a vertex in a directed graph. +class Vertex < ActiveRecord::Base + has_many :sink_edges, :class_name => 'Edge', :foreign_key => 'source_id' + has_many :sinks, :through => :sink_edges, :source => :sink + + has_and_belongs_to_many :sources, + :class_name => 'Vertex', :join_table => 'edges', + :foreign_key => 'sink_id', :association_foreign_key => 'source_id' +end diff --git a/activerecord/test/fixtures/vertices.yml b/activerecord/test/fixtures/vertices.yml new file mode 100644 index 0000000000..8af0593f75 --- /dev/null +++ b/activerecord/test/fixtures/vertices.yml @@ -0,0 +1,4 @@ +<% (1..5).each do |id| %> +vertex_<%= id %>: + id: <%= id %> +<% end %> \ No newline at end of file -- cgit v1.2.3