From 89e98e278abe8564b80953855fcb4bcb9871c51c Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 26 Sep 2011 08:45:53 -0700 Subject: Merge pull request #3030 from htanata/fix_habtm_select_query_method Fix: habtm doesn't respect select query method --- activerecord/lib/active_record/associations/association_scope.rb | 4 ---- .../associations/has_and_belongs_to_many_associations_test.rb | 8 ++++++++ .../test/cases/associations/has_many_associations_test.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index a0efdbc2a4..6cc401e6cc 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -42,10 +42,6 @@ module ActiveRecord select_value ||= options[:uniq] && "DISTINCT #{reflection.quoted_table_name}.*" end - if reflection.macro == :has_and_belongs_to_many - select_value ||= reflection.klass.arel_table[Arel.star] - end - select_value end diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index d8d2a113ff..d1d02c25d5 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -650,6 +650,14 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_respond_to categories(:technology).select_testing_posts.find(:first), :correctness_marker end + def test_habtm_selects_all_columns_by_default + assert_equal Project.column_names, developers(:david).projects.first.attributes.keys + end + + def test_habtm_respects_select_query_method + assert_equal ['id'], developers(:david).projects.select(:id).first.attributes.keys + end + def test_join_table_alias assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL').size end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 682e145828..fdfbcbefac 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -485,6 +485,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, authors(:mary).popular_grouped_posts.length end + def test_default_select + assert_equal Comment.column_names, posts(:welcome).comments.first.attributes.keys + end + + def test_select_query_method + assert_equal ['id'], posts(:welcome).comments.select(:id).first.attributes.keys + end + def test_adding force_signal37_to_load_all_clients_of_firm natural = Client.new("name" => "Natural Company") -- cgit v1.2.3