Jeremy Walker

Software Developer & Social Entrepreneur

belongs_to_enum is released

I've just released a new gem: belongs_to_enum

This adds functionality that I've used in pretty much every Rails app for the last 6 years.

Often, you want to state that model should have (or be) a certain value, selected from a list of options. For example, a user might have a certain security level (normal, admin, superuser etc). It doesn't really make sense to have an extra database table for this, as we want these to be hardcoded values used in our code and not open to change. Adding a new value will involve editing our codebase, so a database table just isn't appropriate.

The general solution is to create a constant such as:

SECURITY_LEVEL = {:normal => 1, :admin => 2, :superuser => 3}

and then have a reference to the constant in the model (e.g. security_level_id).

This gem does exactly that, but in a much quicker, easier and more powerful way. It adds a class method (belongs_to_enum) to ActiveRecord, which does all the mapping for you and gives you extra functionality, such as pretty printing, enumeration etc.

class User < ActiveRecord::Base
  belongs_to_enum :security_level, [:normal, :admin, :superuser]

create_table :users do |t|
  t.integer :security_level_id, :null => false

-User::SecurityLevel.each do |level, level_id|
    #{level} (#{level_id})

=f.select(:security_level_id, User::SecurityLevel.items_for_select)

=@user.security_level_id      #-> 1
=@user.security_level         #-> :normal
=@user.security_level.display #-> "Normal"

You can also use a hash if you want to specify specific keys:

class User < ActiveRecord::Base
  belongs_to_enum :security_level, {:normal => 10, :admin => 20, :superuser => 30}

The gem is very much under active development and the latest version can be found at RubyGems. The source code is at Github - push requests gratefully received.

Share This Post


No-one has posted a comment yet. Be the first!

Post a Comment

I'd love to hear your thoughts :)