Search code examples
ruby-on-railsenumsspecial-characters

Store strings with special characters as enum values in Rails 5


In Rails 5, the enum value type can only accept symbols, i.e. immutable "strings" with letters and digits.

How would you best use enum values in Rails, which have to be shown also with their special characters like e.g. 'Hip-Hop/Rap' included in the enum field genre?


Solution

  • you can use quotes around symbol text if it contains special characters.

    [:classical, :pop, :'Hip-Hop/Rap']