Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix associations associated with a CPK model by id attribute #47923

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -62,7 +62,7 @@ def last_chain_scope(scope, reflection, owner)
table = reflection.aliased_table
primary_key_foreign_key_pairs = primary_key.zip(foreign_key)
primary_key_foreign_key_pairs.each do |join_key, foreign_key|
value = transform_value(owner[foreign_key])
value = transform_value(owner._read_attribute(foreign_key))
scope = apply_scope(scope, table, join_key, value)
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/reflection.rb
Expand Up @@ -550,7 +550,7 @@ def check_eager_loadable!
end

def join_id_for(owner) # :nodoc:
Array(join_foreign_key).map { |key| owner[key] }
Array(join_foreign_key).map { |key| owner._read_attribute(key) }
end

def through_reflection
Expand Down
20 changes: 19 additions & 1 deletion activerecord/test/cases/associations_test.rb
Expand Up @@ -35,14 +35,16 @@
require "models/member"
require "models/membership"
require "models/sharded"
require "models/cpk"
require "models/member_detail"
require "models/organization"


class AssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
:computers, :people, :readers, :authors, :author_addresses, :author_favorites,
:comments, :posts, :sharded_blogs, :sharded_blog_posts, :sharded_comments, :sharded_tags, :sharded_blog_posts_tags
:comments, :posts, :sharded_blogs, :sharded_blog_posts, :sharded_comments, :sharded_tags, :sharded_blog_posts_tags,
:cpk_orders

def test_eager_loading_should_not_change_count_of_children
liquid = Liquid.create(name: "salty")
Expand Down Expand Up @@ -136,6 +138,14 @@ def test_belongs_to_a_model_with_composite_foreign_key_finds_associated_record
assert_equal(blog_post, comment.blog_post)
end

def test_belongs_to_a_cpk_model_by_id_attribute
order = cpk_orders(:cpk_groceries_order_1)
_order_shop_id, order_id = order.id
agreement = Cpk::OrderAgreement.create(order_id: order_id, signature: "signed")

assert_equal(order, agreement.order)
end

def test_belongs_to_a_model_with_composite_primary_key_uses_composite_pk_in_sql
comment = sharded_comments(:great_comment_blog_post_one)

Expand Down Expand Up @@ -164,6 +174,14 @@ def test_has_many_association_with_composite_foreign_key_loads_records
assert_includes(comments, sharded_comments(:great_comment_blog_post_one))
end

def test_cpk_model_has_many_records_by_id_attribute
order = cpk_orders(:cpk_groceries_order_1)
_order_shop_id, order_id = order.id
agreements = 2.times.map { Cpk::OrderAgreement.create(order_id: order_id, signature: "signed") }

assert_equal(agreements.sort, order.order_agreements.to_a.sort)
end

def test_has_many_association_from_a_model_with_query_constraints_different_from_the_association
blog_post = sharded_blog_posts(:great_post_blog_one)
blog_post = Sharded::BlogPostWithRevision.find(blog_post.id)
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/cpk.rb
Expand Up @@ -2,3 +2,4 @@

require_relative "cpk/book"
require_relative "cpk/order"
require_relative "cpk/order_agreement"
2 changes: 2 additions & 0 deletions activerecord/test/models/cpk/order.rb
Expand Up @@ -4,5 +4,7 @@ module Cpk
class Order < ActiveRecord::Base
self.table_name = :cpk_orders
self.primary_key = [:shop_id, :id]

has_many :order_agreements, primary_key: :id
end
end
10 changes: 10 additions & 0 deletions activerecord/test/models/cpk/order_agreement.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Cpk
# This is a non composite primary key model that is associated with `Cpk::Order` via `id` only.
class OrderAgreement < ActiveRecord::Base
self.table_name = :cpk_order_agreements

belongs_to :order, primary_key: :id # foreign key is derived as `order_id`
end
end
7 changes: 7 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -252,6 +252,13 @@
t.string :status
end

create_table :cpk_order_agreements, force: true do |t|
t.integer :order_id
t.string :signature

t.index :order_id
end

create_table :paragraphs, force: true do |t|
t.references :book
end
Expand Down