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

Ensure prefixes are unique #86

Merged
merged 1 commit into from
Nov 25, 2024
Merged

Conversation

samoli
Copy link
Contributor

@samoli samoli commented Nov 25, 2024

When two or more models are defined with the same prefix, PrefixedIds.find will load the model for whichever one was defined last, and could lead to loading an unexpected model for a given prefixed id.

For apps with lots of models it’s quite easy to add the same prefix on two or more models by mistake. It’s also not obvious how to access or list the prefixes already in use, so I think it makes sense for the gem to protect against this.

This PR adds a check when calling has_prefix_id and raises PrefixedIds::Error if the same prefix was already defined for a different model.

When two or more models are defined with the same prefix, `PrefixedIds.find` will load the model for whichever one was defined last, and could lead to loading an unexpected model for a given prefixed id.

For apps with lots of models it’s quite easy to add the same prefix on two or more models by mistake. It’s also not obvious how to access or list the prefixes already in use, so I think it makes sense for the gem to protect against this.

This PR adds a check when calling `has_prefix_id` and raises `PrefixedIds::Error` if the same prefix was already defined for a different model.
@excid3 excid3 merged commit 9e391c1 into excid3:main Nov 25, 2024
@excid3
Copy link
Owner

excid3 commented Nov 25, 2024

Good idea @samoli. I hadn't considered two models using the same prefix. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants