-
Notifications
You must be signed in to change notification settings - Fork 132
Add support for models which use non-primary key columns for lookup (i.e. external_id columns) #199
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
base: main
Are you sure you want to change the base?
Conversation
lib/global_id/identification.rb
Outdated
end | ||
end | ||
|
||
def global_id_method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this method? We can just set the column to be :id
by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I updated the implementation to use the class_attribute
extension from ActiveSupport and brought consistency to the naming
if model_class.global_id_column == :id | ||
model_class.find gid.model_id | ||
else | ||
model_class.find_by model_class.global_id_column => gid.model_id | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 Simplifying on find_by
(or find_by!
) for ActiveRecord users feels nice, but I stopped short of doing that because I recall seeing that globalid doesn't want to make ORM assumptions
85e19f0
to
57d724b
Compare
This allows overriding the column used to define GIDs
Because of Identification.global_id_column, we can keep this internal API much cleaner and avoid mucking with hash state to pass around this column
`Model.primary_key` is actually irrelevant to this feature, the only thing that matters is the model method. For composite keys, or primary keys not named "id", we still get the important value from `Model#id`, the only time we don't is when we need to use a non-PK column.
57d724b
to
13a4a69
Compare
…tribute This simplifies the implementation's API substantially by defining both instance and class methods, and rails/globalid already depends on ActiveSupport
13a4a69
to
b551218
Compare
With this proposal,
GlobalID::Identification
would implementglobal_id_column
to set the desired column for GlobalID to reference on a per-model basisi.e.
I think this is particularly useful for teams in the process of migrating off of auto-incremented primary keys, or are interested in using unsigned GIDs with some level of obscurity that's not attainable without starting to make a bigger change like actually implementing UUIDs as PKs
This PR extends beyond #185's initial implementation and add's support to the default locator to use these alternative columns for look-up