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

Conversation

nvasilevski
Copy link
Contributor

Given a model associated with a composite primary key by id attribute, for example:

Order.primary_key = [:shop_id, :id]
OrderAgreement.primary_key = :id

Order.has_many :order_agreements, primary_key: :id

Accessing the association should perform queries using the id attribute value and not the id as Order's composite primary key.

order = Order.last # => #<Order id: 1, shop_id: 2>

order.order_agreements.to_a

Implementation details

Instead of using public readers to access the values we should use private _read_attribute to avoid logic treating "id" term as "an identifier" returning the whole composite primary key values tuple instead of the id column value

Given a model associated with a composite primary key by `id` attribute,
for example:
```ruby
Order.primary_key = [:shop_id, :id]
OrderAgreement.primary_key = :id

Order.has_many :order_agreements, primary_key: :id
```

Accessing the association should perform queries using
the `id` attribute value and not the `id` as Order's composite primary key.

```ruby
order = Order.last # => #<Order id: 1, shop_id: 2>

order.order_agreements.to_a
```
@eileencodes eileencodes merged commit 3d4dd9f into rails:main Apr 12, 2023
9 checks passed
@eileencodes eileencodes deleted the fix-belongs-to-cpk-by-id-attr-association branch April 12, 2023 16:39
khiav223577 added a commit to khiav223577/deep_pluck that referenced this pull request Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants