Symbolize attribute values in ActiveRecord or Mongoid (nicer enums)
Pull request Compare This branch is 184 commits ahead, 1 commit behind nuxlli:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib rubocop! Dec 6, 2014
spec spec fails Dec 6, 2014
.gitignore Removed the Gemfile.lock because a gem shouldn't have it in the repos… Dec 18, 2012
.rubocop.yml rubocop Sep 20, 2014
.travis.yml we need development deps in tests Jul 31, 2014
Gemfile rubocop and guard Nov 25, 2014
Guardfile rubocop and guard Nov 25, 2014
MIT-LICENSE Updated copyright and removed unneeded stuff Apr 15, 2008 RIP Dec 30, 2014
Rakefile rubocop Sep 20, 2014
symbolize.gemspec fixing mongoid Dec 6, 2014

Symbolize (attribute values)

No longer maintained

Please check this other gems:


ActiveModel & POROs

This plugin introduces an easy way to use symbols for values of attributes. Symbolized attributes return a ruby symbol (or nil) as their value and can be set using :symbols or "strings".



gem install symbolize


gem 'symbolize'


Just use "symbolize :attribute" in your model, and the specified attribute will return symbol values and can be set using symbols (setting string values works, which is important when using forms).

On schema DBs, the attribute should be a string (varchar) column.



class Contact < ActiveRecord::Base

  symbolize :kind, :in => [:im, :mobile, :email], :scopes => true


class Contact
  include Mongoid::Symbolize

  symbolize :kind, :in => [:im, :mobile, :email], :scopes => true

Other examples:

symbolize :os, :in => {
    :linux   => "Linux",
    :mac     => "Mac OS X"
}, :scopes => true

# Allow blank
symbolize :gui,  :in => [:gnome, :kde, :xfce], :allow_blank => true

# Don`t i18n
symbolize :browser, :in => [:firefox, :opera], :i18n => false, :methods => true

# Scopes
symbolize :angry, :in => [true, false], :scopes => true # AR
symbolize :angry, :type => Boolean, :scopes => true # Mongoid

# Don`t validate
symbolize :lang, :in => [:ruby, :js, :c, :erlang], :validate => false

# Default
symbolize :kind, :in => [:admin, :manager, :user], :default => :user


You can provide a hash like for values allowed on the field, e.g.: {:value => "Human text"} or an array of keys to run i18n on. Booleans are also supported. See below.


Set to false to avoid the validation of the input. Useful for a dropdown with an "other" option textfield.

symbolize :color, :in => [:green, :red, :blue], :validate => false

There's is also allow_(blank|nil): As you expect.


If you provide the method option, some fancy boolean methods will be added: In our User example, browser has this option, so you can do:



Its possible to use boolean fields also. Looks better in Mongoid.

# ActiveRecord symbolize :switch, :in => [true, false]

# Mongoid symbolize :switch, :type => Boolean

... switch: "true": On "false": Off "nil": Unknown


If you don`t provide a hash with values, it will try i18n:

        gnome: Gnome Desktop Enviroment
        kde: K Desktop Enviroment
        xfce: XFCE4
        female: Girl
        male: Boy

In some cases, if automatic lookup failes, you may want to try to translate it manully

user.get_gender_text   # here gender is symbolized field

You can skip i18n lookup with :i18n => false

symbolize :style, :in => [:rock, :punk, :funk, :jazz], :i18n => false


With the ':scopes => true' option, you may filter/read/write easily:

User.gender(:female).each ... # => User.where({ :gender => :female })

Now, if you provide the ':scopes => :shallow' option, fancy named scopes will be added to the class directly. In our User example, gender has male/female options, so you can do:

User.female.each ...  # => User.where({ :gender => :female })

You can chain named scopes as well:

User.female.mac => User.all :conditions => { :gender => :female, :so => :mac }

For boolean colums you can use

User.angry     => User.find(:all, :conditions => { :angry => true })
User.not_angry => User.find(:all, :conditions => { :angry => false })

( or with_[attribute] and without_[attribute] )


As the name suggest, the symbol you choose as default will be set in new objects automatically. Mongoid only for now.

symbolize :mood, :in => [:happy, :sad, :euphoric], :default => (MarvinDay ? :sad : :happy) # It may print :happy

Rails Form Example

You may call Class.get_<attribute>_values anywhere to get a nice array. Works nice with dropdowns. Examples:

class Coffee
  symbolize :genetic, :in => [:arabica, :robusta, :blend]

- form_for(@coffee) do |f|
  = f.label :genetic
  = :genetic, Coffee.get_genetic_values

Somewhere on a view:

= select_tag :kind, Coffee.get_genetic_values


Run the adapter independently:

$ rspec spec/symbolize/mongoid_spec.rb
$ rspec spec/symbolize/active_record_spec.rb


This fork: