2

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.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
Ahmad Saleem
  • 138
  • 1
  • 7
  • 1
    As @AndreyDeineko pointed out in his answer it doesn’t matter in this case. Maybe you want to read [this question](http://stackoverflow.com/questions/16621073/when-to-use-symbols-instead-of-strings-in-ruby) for a better understanding of strings and symbols. – Ray Wojciechowski Apr 12 '17 at 09:19

1 Answers1

5

It does not matter, since Enum is an integer in database, and

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']
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145