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 390 390 assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15") 391 391 end 392 392 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 393 426 end -
lib/active_record/associations.rb
old new 1227 1227 1228 1228 def construct_finder_sql_with_included_associations(options, join_dependency) 1229 1229 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} " 1231 1231 sql << join_dependency.join_associations.collect{|join| join.association_join }.join 1232 1232 1233 1233 add_joins!(sql, options, scope) … … 1315 1315 reflections.reject { |r| [ :belongs_to, :has_one ].include?(r.macro) }.length.zero? 1316 1316 end 1317 1317 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(", ") 1321 1321 end 1322 1322 1323 1323 def add_association_callbacks(association_name, options) … … 1472 1472 active_record.table_name 1473 1473 end 1474 1474 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 = {}) 1476 1488 unless @column_names_with_alias 1477 1489 @column_names_with_alias = [] 1478 1490 ([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 1480 1494 end 1481 1495 end 1482 1496 return @column_names_with_alias