Included associations: go deep.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4776 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2006-08-16 18:10:52 +00:00
parent 593f04e6a9
commit 7aaf4867d2
8 changed files with 75 additions and 17 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

5
activerecord/test/fixtures/edge.rb vendored Normal file
View File

@@ -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

6
activerecord/test/fixtures/edges.yml vendored Normal file
View File

@@ -0,0 +1,6 @@
<% (1..4).each do |id| %>
edge_<%= id %>:
id: <%= id %>
source_id: <%= id %>
sink_id: <%= id + 1 %>
<% end %>

View File

@@ -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

9
activerecord/test/fixtures/vertex.rb vendored Normal file
View File

@@ -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

View File

@@ -0,0 +1,4 @@
<% (1..5).each do |id| %>
vertex_<%= id %>:
id: <%= id %>
<% end %>