mirror of https://github.com/rails/rails
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:
commit
5262ec5900
|
@ -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
|
||||
|
|
|
@ -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!
|
||||
|
|
Loading…
Reference in New Issue