mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix honoring :primary_key option when joining or eager loading a belongs_to association
[#765 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
parent
a509b0b18f
commit
63026541b2
3 changed files with 15 additions and 2 deletions
|
@ -324,7 +324,7 @@ module ActiveRecord
|
||||||
klass = klass_name.constantize
|
klass = klass_name.constantize
|
||||||
|
|
||||||
table_name = klass.quoted_table_name
|
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
|
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|
|
||||||
|
|
|
@ -2031,7 +2031,7 @@ module ActiveRecord
|
||||||
[aliased_table[foreign_key].eq(parent_table[reflection.options[:primary_key] || parent.primary_key])]
|
[aliased_table[foreign_key].eq(parent_table[reflection.options[:primary_key] || parent.primary_key])]
|
||||||
end
|
end
|
||||||
when :belongs_to
|
when :belongs_to
|
||||||
[aliased_table[reflection.klass.primary_key].eq(parent_table[options[:foreign_key] || reflection.primary_key_name])]
|
[aliased_table[options[:primary_key] || reflection.klass.primary_key].eq(parent_table[options[:foreign_key] || reflection.primary_key_name])]
|
||||||
end
|
end
|
||||||
|
|
||||||
unless klass.descends_from_active_record?
|
unless klass.descends_from_active_record?
|
||||||
|
|
|
@ -32,6 +32,12 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
|
assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_belongs_to_with_primary_key_joins_on_correct_column
|
||||||
|
sql = Client.joins(:firm_with_primary_key).to_sql
|
||||||
|
assert_no_match /"firm_with_primary_keys_companies"\."id"/, sql
|
||||||
|
assert_match /"firm_with_primary_keys_companies"\."name"/, sql
|
||||||
|
end
|
||||||
|
|
||||||
def test_proxy_assignment
|
def test_proxy_assignment
|
||||||
account = Account.find(1)
|
account = Account.find(1)
|
||||||
assert_nothing_raised { account.firm = account.firm }
|
assert_nothing_raised { account.firm = account.firm }
|
||||||
|
@ -61,6 +67,13 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_equal apple.name, citibank.firm_name
|
assert_equal apple.name, citibank.firm_name
|
||||||
end
|
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
|
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)
|
||||||
|
|
Loading…
Reference in a new issue