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 validating enum to work with both Rails 6 and Rails 6.1 #34

Open
benreyn opened this issue Nov 3, 2021 · 0 comments
Open

Fix validating enum to work with both Rails 6 and Rails 6.1 #34

benreyn opened this issue Nov 3, 2021 · 0 comments

Comments

@benreyn
Copy link
Contributor

benreyn commented Nov 3, 2021

Rails 6.1.1 broke the validating_enum code with the following commit (this appears to have been committed directly to main and there is no associated PR): rails/rails@3813021

Per the commit, this was in response to issue rails/rails#41118. In the issue the user is setting up an enum mapping a class constant to an integer. The above referenced commit changes the predicate convenience implementation to use the X_for_database method which will call serialize on the underlying EnumType. This is surprising to us because we expect serialize to only be called when the data is going to be sent to the DB. Our custom ValidateableEnumType type included a guard to raise a KeyError for invalid data in this case; a final business logic check prior to having the DB yell at us or worse silently do something unexpected (looking at you "invalid".to_i => nil).

We are not sure yet how best to approach updating this code for Rails 6.1. There's currently an open PR which looks to make this type of validation a first-class feature, maybe, in Rails 7: rails/rails#41730. But this is still pending as of the writing of this comment.

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

No branches or pull requests

1 participant