Skip to content

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

@benreyn

Description

@benreyn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions