Search code examples
ruby-on-railsrubyruby-on-rails-4enums

Rails enum symbol vs string


I have just started working on a new rails project, and I have noticed that the previous developer has used strings instead of symbols as keys for the enum. The example is as follows:

enum event_type: {
  'Tournament'    => 1,
  'Practice Game' => 2
}

I am just curious about the possible advantages/disadvantages of using strings instead of symbols.


Solution

  • The mappings are exposed through a class method with the pluralized attribute name, which return the mapping in a HashWithIndifferentAccess

    so whether you used strings or symbols both of the following will work:

    MyModel.event_types[:Tournament]
    MyModel.event_types['Tournament']