ruby-on-railsactiverecordruby-on-rails-plugins

Why is it bad to use models as enumerations?


I was just watching a preview of a session at Aloha on Rails called "You're Doing it Wrong".

In the short preview, the presenter mentions using ActiveRecord models as enumerations (I assume he means plugins like enumerate_by). This seems like reasonable idea to me, what are the problems? Is it just the overhead required by the additional objects?


Solution

  • He's a bit hyperbolic and idiomatic in his presentation. His point is sound, though.

    There is development overhead to putting static data into a database: you'll need to make sure that all migrations are run in all environments (including every time you db:test:clone), you'll need to load the data every time you work with your code (eg, even in irb), you may run into load order problems. In short, it's not free, and we don't want to incur unnecessary development and support costs.

    However, there can be benefits to doing it. The data may want to be shared between code written in different languages; the code might be complicated and require an expensive test cycle before each and every deployment; you may want to create database constraints the data; you may have extensive or structured metadata you want to associate with the enumeration; and so on.

    These conditions aren't common, though. For most enumerations, named constants, a hash, or even just a convention using symbols is sufficient.