# How to implement status codes in DB and Rails

**URL:** <https://www.ruby-forum.com/t/how-to-implement-status-codes-in-db-and-rails/75239>\
**Category:** Rails\
**Created:** [November 8, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/how-to-implement-status-codes-in-db-and-rails/75239 "2006-11-08T12:46:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 8, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/how-to-implement-status-codes-in-db-and-rails/75239/1 "2006-11-08T12:46:07Z")

</div>

This is more of a design issue I’m wrestling with, and was wondering  
what other people are doing.

I have a products table with status values of In Stock, Out of Stork or  
Discontinued. This is unlikely to change any time soon.

Approach A, is to have thses actual values in the status column, at  
least the meaning is very apparent, yet querying for products with a  
specific status could be problematic if due to a business decision the  
description had to be changed, e.g. Out of Stock became Not in Stock.

Approach B, is to have a code that represents the status, such as I, O,  
D, querying would work, and their descriptions could change. But when  
the data was displayed the code would have to be translated to a  
description every time.

Approach C, have a new status look up table, which would have an id  
column and a description, and the products table would then have a  
status id, which if the description was required would include the  
status table in the find. This seems a bit over the top but maybe is the  
cleanest way and status can be added and descriptions changed without  
any changes to application code.

Approach D, have a new table with status codes I, O, D but that would  
mean hard coding a lot of the queries as find\_by\_sql, to have the table  
join, as there is no id column on the status table.

Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 8, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/how-to-implement-status-codes-in-db-and-rails/75239/2 "2006-11-08T15:11:17Z")

</div>

Andrew C. wrote:

> Approach C, have a new status look up table, which would have an id  
> column and a description, and the products table would then have a  
> status id, which if the description was required would include the  
> status table in the find. This seems a bit over the top but maybe is the  
> cleanest way and status can be added and descriptions changed without  
> any changes to application code.

This works well in conjunction with Trevor S.’ acts\_as\_enumerated  
plugin. Using this, statuses are stored in the DB as ids, tested in  
code using symbolic name fields, and displayed in views as either the  
name field or a separate description field:

[http://svn.protocool.com/rails/plugins/enumerations\_mixin/trunk/](http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk/)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 8, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/how-to-implement-status-codes-in-db-and-rails/75239/3 "2006-11-08T20:18:18Z")

</div>

On Nov 8, 2006, at 3:46 AM, Andrew C. wrote:

> specific status could be problematic if due to a business decision the  
> status id, which if the description was required would include the  
> Andrew  
> 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 Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 8, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/how-to-implement-status-codes-in-db-and-rails/75239/4 "2006-11-08T21:23:14Z")

</div>

Andrew C. wrote:

> Approach C, have a new status look up table, which would have an id  
> column and a description, and the products table would then have a  
> status id, which if the description was required would include the  
> status table in the find. This seems a bit over the top but maybe is the  
> cleanest way and status can be added and descriptions changed without  
> any changes to application code.

Approach C is the only way that conforms to 3NF.

Joe
