Though you might already have solved your issue at hand, I've recently
ran into a similar situation and written a small plugin for it,
columns are backed by integers, but in ruby you can treat them using
symbols or whatever:
class User < ActiveRecord::Base
as_enum :status, { :single => 0, :married => 1, :divorced => 2 }
end
Then create an integer column:
add_column :users, :status_cd, :integer
It's then possible to easily access these values using @user.status:
@user = User.new
@user.status = :married # => implies @user.status_cd = 1
For my problem I also required some shorthands to check for values, so
I've added also <symbol>? methods:
@user.married? # => true
@user.single? # => false
The code lives at github http://github.com/lwe/simple_enum/tree/master
any feedback is appreciated
cheers, lukas
On May 7, 10:43 am, Vipin <[email protected]> wrote:
> in a database table if there is a field which has a certain set of
> fixed values. for example
> staus => {Single, Married, Divorced }
> OR
> state => {California, Albama, Olaska ...}
>
> so what should be preferred way out of the following for storing the
> values
>
> 1. Keep the field as "string(Rails)" VARCHAR(MySQL) itself ....and
> while showing the field just show the field value.
>
> 2. Keep the field internally as a code like {:california =>
> 01, :albama => 02, washington => 03 ....} but while showing the state
> show only the corresponding state.
>
> By using option 2, a certain disadvantage is extra computation time
> required to find out corresponding state name based on code when
> showing the state field to user. But an advantage could be in terms of
> smaller database. In my opinion, saving 01 as an integer could save
> significant space than storing "california" if number of records
> happen to be in tens of thousands .
>
> please suggest ??
>
> vipin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---