Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4776

Show
Ignore:
Timestamp:
08/16/06 18:10:52 (3 years ago)
Author:
bitsweat
Message:

Included associations: go deep.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/associations.rb

    r4758 r4776  
    13941394              unless join_dependency.table_aliases[aliased_table_name].zero? 
    13951395                # if the table name has been used, then use an alias 
    1396                 @aliased_table_name = cascade_alias 
     1396                @aliased_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}" 
    13971397                table_index = join_dependency.table_aliases[aliased_table_name] 
    1398                 join_dependency.table_aliases[@aliased_table_name] += 1 
     1398                join_dependency.table_aliases[aliased_table_name] += 1 
    13991399                @aliased_table_name = @aliased_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0 
    14001400              else 
     
    14071407                  @aliased_join_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}_join" 
    14081408                  table_index = join_dependency.table_aliases[aliased_join_table_name] 
     1409                  join_dependency.table_aliases[aliased_join_table_name] += 1 
    14091410                  @aliased_join_table_name = @aliased_join_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0 
     1411                else 
     1412                  join_dependency.table_aliases[aliased_join_table_name] += 1 
    14101413                end 
    1411                 join_dependency.table_aliases[aliased_join_table_name] += 1 
    14121414              end 
    14131415            end 
     
    14201422                     aliased_join_table_name, 
    14211423                     options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key, 
    1422                      reflection.active_record.table_name, reflection.active_record.primary_key] + 
     1424                     parent.aliased_table_name, reflection.active_record.primary_key] + 
    14231425                  " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ 
    14241426                     table_name_and_alias, aliased_table_name, klass.primary_key, 
     
    14581460                            when :belongs_to 
    14591461                              first_key  = primary_key 
    1460                               second_key = options[:foreign_key] || klass.to_s.classify.foreign_key 
     1462                              second_key = source_reflection.options[:foreign_key] || klass.to_s.classify.foreign_key 
    14611463                            when :has_many 
    14621464                              first_key  = through_reflection.klass.to_s.classify.foreign_key 
    14631465                              second_key = options[:foreign_key] || primary_key 
    14641466                          end 
    1465                            
    14661467                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s "  % [ 
    1467                             table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, 
    1468                             through_reflection.primary_key_name, 
     1468                            table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),  
     1469                            aliased_join_table_name, through_reflection.primary_key_name, 
    14691470                            parent.aliased_table_name, parent.primary_key] + 
    14701471                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ 
     
    15321533                instance_eval("%@#{sql.gsub('@', '\@')}@") 
    15331534              end 
    1534                
    1535             private 
    1536               def cascade_alias 
    1537                 active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}" 
    1538               end 
    15391535          end 
    15401536        end 
  • trunk/activerecord/test/associations_cascaded_eager_loading_test.rb

    r4758 r4776  
    1212 
    1313class CascadedEagerLoadingTest < Test::Unit::TestCase 
    14   fixtures :authors, :mixins, :companies, :posts, :categorizations, :topics 
     14  fixtures :authors, :mixins, :companies, :posts, :topics 
    1515 
    1616  def test_eager_association_loading_with_cascaded_two_levels 
     
    9595    end 
    9696  end 
    97    
     97 
    9898  def test_eager_association_loading_of_stis_with_multiple_references 
    9999    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') 
     
    104104    end 
    105105  end 
    106    
    107   def test_eager_association_loading_with_recursive_cascaded_three_levels 
     106 
     107  def test_eager_association_loading_with_recursive_cascading_three_levels_has_many 
    108108    root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:children=>{:children=>:children}}, :order => 'mixins.id') 
    109109    assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.children.first.children.first.children.first } 
    110110  end 
     111 
     112  def test_eager_association_loading_with_recursive_cascading_three_levels_has_one 
     113    root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:first_child=>{:first_child=>:first_child}}, :order => 'mixins.id') 
     114    assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.first_child.first_child.first_child } 
     115  end 
     116 
     117  def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to 
     118    leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:parent=>{:parent=>:parent}}, :order => 'mixins.id DESC') 
     119    assert_equal mixins(:recursively_cascaded_tree_1), assert_no_queries { leaf_node.parent.parent.parent } 
     120  end 
    111121end 
     122 
     123 
     124require 'fixtures/vertex' 
     125require 'fixtures/edge' 
     126class CascadedEagerLoadingTest < Test::Unit::TestCase 
     127  fixtures :edges, :vertices 
     128 
     129  def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through 
     130    source = Vertex.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id') 
     131    assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first } 
     132  end 
     133 
     134  def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many 
     135    sink = Vertex.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC') 
     136    assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first } 
     137  end 
     138end 
  • trunk/activerecord/test/fixtures/db_definitions/schema.rb

    r4594 r4776  
    4040    t.column :favorite_author_id, :integer 
    4141  end 
     42 
     43  create_table :vertices, :force => true do |t| 
     44    t.column :label, :string 
     45  end 
     46 
     47  create_table :edges, :force => true do |t| 
     48    t.column :source_id, :integer, :null => false 
     49    t.column :sink_id,   :integer, :null => false 
     50  end 
     51  add_index :edges, [:source_id, :sink_id], :unique => true 
    4252end 
  • trunk/activerecord/test/fixtures/mixin.rb

    r4758 r4776  
    1313class RecursivelyCascadedTreeMixin < Mixin 
    1414  acts_as_tree :foreign_key => "parent_id" 
     15  has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id 
    1516end 
    1617