Skip to content

Commit

Permalink
Convert :primary_key in association to a string before comparing to c…
Browse files Browse the repository at this point in the history
…olumn names, so that for example :primary_key => :another_pk works as well [#5605 state:resolved]
  • Loading branch information
odorcicd authored and tenderlove committed Oct 30, 2010
1 parent 67a3a70 commit cc97429
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/association_preload.rb
Expand Up @@ -320,7 +320,7 @@ def preload_belongs_to_association(records, reflection, preload_options={})
klass = klass_name.constantize klass = klass_name.constantize


table_name = klass.quoted_table_name table_name = klass.quoted_table_name
primary_key = reflection.options[:primary_key] || klass.primary_key primary_key = (reflection.options[:primary_key] || klass.primary_key).to_s
column_type = klass.columns.detect{|c| c.name == primary_key}.type column_type = klass.columns.detect{|c| c.name == primary_key}.type


ids = _id_map.keys.map do |id| ids = _id_map.keys.map do |id|
Expand Down
Expand Up @@ -81,6 +81,13 @@ def test_eager_loading_with_primary_key
assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key") assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key")
end end


def test_eager_loading_with_primary_key_as_symbol
Firm.create("name" => "Apple")
Client.create("name" => "Citibank", :firm_name => "Apple")
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key_symbols)
assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key_symbols")
end

def test_no_unexpected_aliasing def test_no_unexpected_aliasing
first_firm = companies(:first_firm) first_firm = companies(:first_firm)
another_firm = companies(:another_firm) another_firm = companies(:another_firm)
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/company.rb
Expand Up @@ -107,6 +107,7 @@ class Client < Company
belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of" belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1] belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1]
belongs_to :firm_with_primary_key, :class_name => "Firm", :primary_key => "name", :foreign_key => "firm_name" belongs_to :firm_with_primary_key, :class_name => "Firm", :primary_key => "name", :foreign_key => "firm_name"
belongs_to :firm_with_primary_key_symbols, :class_name => "Firm", :primary_key => :name, :foreign_key => :firm_name
belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true


# Record destruction so we can test whether firm.clients.clear has # Record destruction so we can test whether firm.clients.clear has
Expand Down

0 comments on commit cc97429

Please sign in to comment.