Merge pull request #40815 from kamipo/fix_where_with_custom_primary_key

Fix `where` with custom primary key for belongs_to association
This commit is contained in:
Ryuta Kamizono 2020-12-12 09:07:50 +09:00 committed by GitHub
commit 5262ec5900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -31,8 +31,8 @@ module ActiveRecord
end
def convert_to_id(value)
if value.respond_to?(:id)
value.id
if value.respond_to?(primary_key)
value.public_send(primary_key)
else
value
end

View File

@ -29,7 +29,7 @@ require "models/parrot"
class BelongsToAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :topics,
:developers_projects, :computers, :authors, :author_addresses,
:posts, :tags, :taggings, :comments, :sponsors, :members
:essays, :posts, :tags, :taggings, :comments, :sponsors, :members
def test_belongs_to
client = Client.find(3)
@ -40,6 +40,10 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
end
end
def test_where_with_custom_primary_key
assert_equal [authors(:david)], Author.where(owned_essay: essays(:david_modest_proposal))
end
def test_assigning_belongs_to_on_destroyed_object
client = Client.create!(name: "Client")
client.destroy!