Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use supplied primary key when eager-loading belongs_to associations r…

…ather than default primary key

[#765]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 715b34fdff1e1e93204e57737d69f8d365e17504 1 parent 00b95eb
@joeyAghion joeyAghion authored jeremy committed
View
2  activerecord/lib/active_record/association_preload.rb
@@ -323,7 +323,7 @@ def preload_belongs_to_association(records, reflection, preload_options={})
klass = klass_name.constantize
table_name = klass.quoted_table_name
- primary_key = klass.primary_key
+ primary_key = reflection.options[:primary_key] || klass.primary_key
column_type = klass.columns.detect{|c| c.name == primary_key}.type
ids = id_map.keys.map do |id|
if column_type == :integer
View
7 activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -66,6 +66,13 @@ def test_natural_assignment_with_primary_key
assert_equal apple.name, citibank.firm_name
end
+ def test_eager_loading_with_primary_key
+ apple = Firm.create("name" => "Apple")
+ citibank = Client.create("name" => "Citibank", :firm_name => "Apple")
+ citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key)
+ assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key")
+ end
+
def test_no_unexpected_aliasing
first_firm = companies(:first_firm)
another_firm = companies(:another_firm)
Please sign in to comment.
Something went wrong with that request. Please try again.