Skip to content
enum_fu is a rails plugin with which you can use integer fields in DB as enum typed in ActiveRecord
Ruby
Find file
Pull request Compare This branch is 4 commits ahead, 2 commits behind ikspres:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
README.rdoc
Rakefile
init.rb
install.rb
uninstall.rb

README.rdoc

EnumFu

With enum_fu, you can use integer fields in DB as enum typed in ActiveRecord.

* faster operation in DB
* easy coding

Install

rails plugin install git://github.com/galetahub/enum_fu.git

Usage

In database:

create_table 'users' do |t|
  t.column 'role', :integer, :limit => 2
end

In model:

class User < ActiveRecord::Base
  acts_as_enum :role, [:customer, :admin]
end

Examples

Create new record:

u = User.new(:role => :customer) 
u.save

Get value:

u.role				# :customer
u.role_before_type_cast		# 0

Set value:

u.role = :user

Get the integer value for an enum value

User.role(:user)		# 1

List of all enum values:

User::ROLE			# [:customer, :admin]

In a view:

<% form_for :customer, :url => {:action => 'create'} do |f| %>
    <%= f.select :role, Customer::ROLE %>
<% end %>

In a controller:

u = User.new(params[:customer])
Something went wrong with that request. Please try again.