Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

belongs_to associations now honor :primary_key option for joins

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 00b95eb265ff606ae0a7b650265aa3c6fd43a1da 1 parent 03d5d0b
Ernie Miller authored February 11, 2010 jeremy committed March 29, 2010
2  activerecord/lib/active_record/associations.rb
@@ -2173,7 +2173,7 @@ def association_join
2173 2173
                   " #{join_type} %s ON %s.%s = %s.%s " % [
2174 2174
                      table_name_and_alias,
2175 2175
                      connection.quote_table_name(aliased_table_name),
2176  
-                     reflection.klass.primary_key,
  2176
+                     reflection.options[:primary_key] || reflection.klass.primary_key,
2177 2177
                      connection.quote_table_name(parent.aliased_table_name),
2178 2178
                      options[:foreign_key] || reflection.primary_key_name
2179 2179
                     ]
6  activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -31,6 +31,12 @@ def test_belongs_to_with_primary_key
31 31
     assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
32 32
   end
33 33
 
  34
+  def test_belongs_to_with_primary_key_joins_on_correct_column
  35
+    sql = Client.send(:construct_finder_sql, :joins => :firm_with_primary_key)
  36
+    assert sql !~ /\.id/
  37
+    assert sql =~ /\.name/
  38
+  end
  39
+
34 40
   def test_proxy_assignment
35 41
     account = Account.find(1)
36 42
     assert_nothing_raised { account.firm = account.firm }

0 notes on commit 00b95eb

Please sign in to comment.
Something went wrong with that request. Please try again.