How to implement status codes in DB and Rails

You can use a state pattern for this and it works out pretty clean. There is also the acts_as_state_machine plugin you may want to check out. But here is a simple example of the state pattern used in credit card settlements.

# id :integer(11) not null, primary key
# name :string(80) default(), not null
# description :text

class SettlementState < ActiveRecord::Base
   has_many :settlements,
            :order => 'settlements.id'
end

class Settlement < ActiveRecord::Base

   belongs_to :settlement_state
   belongs_to :ledger

   validates_presence_of :xaction
   validates_presence_of :message

   def authorized
     self.settlement_state = SettlementState.find_by_name('authorized')
   end

   def captured
     self.settlement_state = SettlementState.find_by_name('captured')
   end

   def voided
     self.settlement_state = SettlementState.find_by_name('voided')
   end

   def credited
     self.settlement_state = SettlementState.find_by_name('credited')
   end

   def errored
     self.settlement_state = SettlementState.find_by_name('errored')
   end

   def settled
     self.settlement_state = SettlementState.find_by_name('settled')
   end

   def is_authorized?
     self.settlement_state.name == 'authorized'
   end

   def is_captured?
     self.settlement_state.name == 'captured'
   end

   def is_voided?
     self.settlement_state.name == 'voided'
   end

   def is_credited?
     self.settlement_state.name == 'credited'
   end

   def is_errored?
     self.settlement_state.name == 'errored'
   end

   def is_settled?
     self.settlement_state.name == 'settled'
   end
end

Cheers-
-- Ezra Zygmuntowicz-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)