Skip to content

Suggestion: Option to raise error when record has non-enumerized value? #453

Closed as not planned
@jzisser9

Description

@jzisser9

TLDR: A configuration option to cause Enumerize to raise an error when a record with an invalid enumerized attribute would be very helpful!


Hello! My team and I were recently investigating an issue that turned out to be due to the way Enumerize behaves with database records with an invalid value. For example, we have a database table with a string column, example, with enumerized values being ['foo', 'bar', 'baz']. We had some records where example was set to baw. We then made an ActiveRecord query:

Thing.where(example: 'baw').count
#=> 70

Thing.where(example: 'baw').last
<Thing:#abc123
...
example: nil
>

This was caused by a data migration we ran on this table before adding the enumerize keyword to the model. The data migration populated records in the database with a value we didn't realize at the time was invalid. So once we added Enumerize to the model, we introduced a bug that causes the behavior to break silently.

It would be really cool if there was a configuration option that causes Enumerize to raise an error when fetching a record which has an invalid value for an enumerized attribute. It probably makes the most sense to default it to false to preserve current behavior, but I'd leave that up to the implementer.

Thanks for your consideration!

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