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

Accept composite primary key in id= #47720

Merged
merged 1 commit into from
Mar 21, 2023
Merged

Conversation

paarthmadan
Copy link
Contributor

Motivation / Background

This is part of the ongoing effort to support composite primary keys.

A high-volume code path (often in tests) is setting a model's primary key using the accessor method. This PR modifies the API of id= to accept an array in composite primary key contexts.

Implementation

Use branching to either write the value to the single attribute, or values to the list of attributes corresponding to the primary key.

Combining these branches using Array removes some handling on when the @primary_key is nil because Array(nil) resolves to []. A test fails in that scenario, so I've opted to use branching.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@paarthmadan paarthmadan changed the title Accept composite primary key in id= Accept composite primary key in id= Mar 20, 2023
Copy link
Contributor

@nvasilevski nvasilevski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

book.id = [1, 2]
book.save!

assert_equal [1, 2], book.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if it would be useful to additionally assert for every part of the pk like

assert_equal(1, book.author_id)
assert_equal(2, book.number)

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

3 participants