Created
April 22, 2023 20:28
-
-
Save nsmmrs/ab758facd06054816f947013d97dddb2 to your computer and use it in GitHub Desktop.
Rails "string_enum" Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationRecord | |
def self.string_enum(column_name, values, **options) | |
enum column_name => values.map(&:to_s).index_by(&:itself), **options | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This allows declaring an
enum
on a String column as easily (and clearly) as with an Integer column.For example:
(As opposed to:)