From 9b9a679f92ffd93ca82356d912f3245b73e19a4a Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Thu, 11 Feb 2010 12:30:33 -0500 Subject: [PATCH] belongs_to associations now honor :primary_key option for joins --- activerecord/lib/active_record/associations.rb | 2 +- .../associations/belongs_to_associations_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 3ff8aba..eeaccd9 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -2173,7 +2173,7 @@ module ActiveRecord " #{join_type} %s ON %s.%s = %s.%s " % [ table_name_and_alias, connection.quote_table_name(aliased_table_name), - reflection.klass.primary_key, + reflection.options[:primary_key] || reflection.klass.primary_key, connection.quote_table_name(parent.aliased_table_name), options[:foreign_key] || reflection.primary_key_name ] diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 9f3945f..2ff5e71 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -30,6 +30,12 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name) assert_equal companies(:first_firm).name, client.firm_with_primary_key.name end + + def test_belongs_to_with_primary_key_joins_on_correct_column + sql = Client.send(:construct_finder_sql, :joins => :firm_with_primary_key) + assert sql !~ /\.id/ + assert sql =~ /\.name/ + end def test_proxy_assignment account = Account.find(1) -- 1.6.4.4