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

Ticket #7147: options_select_working_with_eager_loading.diff

File options_select_working_with_eager_loading.diff, 5.1 kB (added by paolo.negri, 4 years ago)
  • test/associations/eager_test.rb

    old new  
    390390      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15") 
    391391    end 
    392392  end 
     393 
     394  def test_select_with_include 
     395    posts = Post.find(:all, :select => "title") 
     396    assert_equal ["title"], posts[0].attributes.keys 
     397    posts = Post.find(:all, :include => :comments) 
     398    post = posts.find { |p| p.id == 1 } 
     399    assert_equal 2, post.comments.size 
     400    assert post.comments.include?(comments(:greetings)) 
     401    post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'") 
     402    assert_equal 2, post.comments.size 
     403    assert_equal ["title", "body", "taggings_count", "type", "id", "author_id"].to_set, post.attributes.keys.to_set 
     404    post.comments.each do |comment| 
     405      assert_equal ["body", "type", "post_id", "id"].to_set, comment.attributes.keys.to_set 
     406    end 
     407    post = Post.find(:first, :include => :comments, :select => 'posts.id, posts.title' ,:conditions => "posts.title = 'Welcome to the weblog'") 
     408    assert_equal 2, post.comments.size 
     409    assert_equal ["title", "id"].to_set, post.attributes.keys.to_set 
     410    post.comments.each do |comment| 
     411      assert_equal ["id"].to_set, comment.attributes.keys.to_set 
     412    end 
     413    post = Post.find(:first, :include => :comments, :select => 'posts.id, posts.title, comments.body, comments.type' ,:conditions => "posts.title = 'Welcome to the weblog'") 
     414    assert_equal 2, post.comments.size 
     415    assert_equal ["title", "id"].to_set, post.attributes.keys.to_set 
     416    post.comments.each do |comment| 
     417      assert_equal ["id", "body", "type"].to_set, comment.attributes.keys.to_set 
     418    end 
     419    post = Post.find(:first, :include => :comments, :select => 'posts.*, comments.body, comments.type' ,:conditions => "posts.title = 'Welcome to the weblog'") 
     420    assert_equal 2, post.comments.size 
     421    assert_equal ["title", "body", "taggings_count", "type", "id", "author_id"].to_set, post.attributes.keys.to_set 
     422    post.comments.each do |comment| 
     423      assert_equal ["body", "type", "post_id", "id"].to_set, comment.attributes.keys.to_set 
     424    end 
     425  end 
    393426end 
  • lib/active_record/associations.rb

    old new  
    12271227 
    12281228        def construct_finder_sql_with_included_associations(options, join_dependency) 
    12291229          scope = scope(:find) 
    1230           sql = "SELECT #{column_aliases(join_dependency)} FROM #{(scope && scope[:from]) || options[:from] || table_name} " 
     1230          sql = "SELECT #{column_aliases(join_dependency, options)} FROM #{(scope && scope[:from]) || options[:from] || table_name} " 
    12311231          sql << join_dependency.join_associations.collect{|join| join.association_join }.join 
    12321232  
    12331233          add_joins!(sql, options, scope) 
     
    13151315          reflections.reject { |r| [ :belongs_to, :has_one ].include?(r.macro) }.length.zero? 
    13161316        end 
    13171317 
    1318         def column_aliases(join_dependency
    1319           join_dependency.joins.collect{|join| join.column_names_with_alias.collect{|column_name, aliased_name| 
    1320               "#{join.aliased_table_name}.#{connection.quote_column_name column_name} AS #{aliased_name}"}}.flatten.join(", ") 
     1318        def column_aliases(join_dependency, options = {}
     1319          join_dependency.joins.collect{|join| join.column_names_with_alias(options).collect{|column_name, aliased_name| 
     1320              "#{join.aliased_table_name}.#{connection.quote_column_name column_name} AS #{aliased_name}"}}.flatten.join(", ")  
    13211321        end 
    13221322 
    13231323        def add_association_callbacks(association_name, options) 
     
    14721472              active_record.table_name 
    14731473            end 
    14741474 
    1475             def column_names_with_alias 
     1475            def parse_select(select_options) 
     1476              return false if select_options.nil? 
     1477              tokens = select_options.gsub(' ', '').split(',') 
     1478              if tokens.size > 1 
     1479                parsed = tokens.inject(Hash.new() {[]}) {|hash, token| tab, col = token.split('.'); hash[tab] = hash[tab] << col; hash} 
     1480                return false if parsed.to_a.flatten.member? '*' 
     1481                return parsed 
     1482              else 
     1483                false 
     1484              end 
     1485            end 
     1486 
     1487            def column_names_with_alias(options = {}) 
    14761488              unless @column_names_with_alias 
    14771489                @column_names_with_alias = [] 
    14781490                ([primary_key] + (column_names - [primary_key])).each_with_index do |column_name, i| 
    1479                   @column_names_with_alias << [column_name, "#{ aliased_prefix }_r#{ i }"] 
     1491                  unless column_name != primary_key && (sel = parse_select(options[:select])) && !sel[table_name].member?(column_name) 
     1492                    @column_names_with_alias << [column_name, "#{ aliased_prefix }_r#{ i }"] 
     1493                  end 
    14801494                end 
    14811495              end 
    14821496              return @column_names_with_alias