Forum: Ruby on Rails what database field type should i use ??

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ff834f7363c1dc9c48b525fc48c67912?d=identicon&s=25 Vipin (Guest)
on 2009-05-07 10:45
(Received via mailing list)
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-07 13:46
(Received via mailing list)
In the case of method 2 where would you store the tens of thousands of
strings if not in the database?
Colin

2009/5/7 Vipin <sh.vipin@gmail.com>
613340c5fef56ad6fc3bf8cc617266ae?d=identicon&s=25 Andrew Porter (Guest)
on 2009-05-07 14:03
(Received via mailing list)
Vipin 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

I use ENUM() columns in mysql - rails treats them as strings so
everything works (apart from schema dumps)
1e7782e67bb34c9c67ed19d5cde5f4eb?d=identicon&s=25 Tom Z Meinlschmidt (Guest)
on 2009-05-07 14:50
(Received via mailing list)
just realize...

in the case od 1st method:
everything is simple, but db grows.. but... you made some typo mistake
(eg Albama instead of Alabama) and what then.. repair entire table to
get the correct results? weird

2nd method:
I'm using it for a small enumeration lists (not hundreds/thousands items
- then is better to use db enumeratin methods, like countries table and
country_id column)

in app/model/some_model.rb

@@enum_list = %w( value1 value2 value3 )

# to use in erb templates for form.select() helper
def self.enum_list_for_select
   @@enum_list.enum_with_index.collect{|k,v| [k,v]}
end

def enum_list_to_text
   self.enum_list.nil? ? 'undefined' : @@enum_list[self.enum_list]
end

tom

Vipin wrote:
> while showing the field just show the field value.
> happen to be in tens of thousands .
>
> please suggest ??
>
> vipin
>
>

--
===============================================================================
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com  www.maxwellrender.cz  www.lightgems.cz
===============================================================================
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-07 15:25
(Received via mailing list)
2009/5/7 Andrew Porter <andy@defsdoor.org>

>
> I use ENUM() columns in mysql - rails treats them as strings so
> everything works (apart from schema dumps)
>

Sorry, I misunderstood the question, I assumed you wanted to know how to
store the country names in a countries table.  I did not realise that in
option 1 you meant storing the string in every record that references
country, though that is what you said, I did not read it carefully.  Now
that I understand, I would suggest some variant of option 2, store an id
in
each record referencing a country and determine the string by an
enumeration
or table lookup later.  Worrying too much about computing time during
development is a mugs game, an application always ends up with most of
its
computing time in an area you do not expect.  Worry about optimisation
later
when (or more likely if) a particular area becomes a problem.

Colin
08182d53361d7ecbb602cb62c8fafe1a?d=identicon&s=25 Billee D. (Guest)
on 2009-05-07 17:10
(Received via mailing list)
Sometimes it''s a matter of taste or design constraint, but why not
use a small join table and a foreign key? ENUM is also a great choice,
as these guys have pointed out, but sometimes there is a bit of
overhead -- but you shouldn't worry about that until it becomes an
issue. Personally, I find it easier to maintain a simple join table
and FK relationships than to mess with ENUM field types.

I don't know if this is still applicable, but it seems like there is a
bit of data massaging in Rails for the ENUM type (Rails converts it
internally to VARCHAR):

http://lists.rubyonrails.org/pipermail/rails/2005-...

I like to let the database do as much work as it can, :-)

HTH!

Billee D.
1e7782e67bb34c9c67ed19d5cde5f4eb?d=identicon&s=25 Tom Z Meinlschmidt (Guest)
on 2009-05-07 18:54
(Received via mailing list)
It's about performance.

if you'll have marriage status (single/married/divorced/etc) in table
marriagestatuses, and using belongs_to/has_many via marriagestatus_id,
and not using :include => marriagestatuses in you 'queries', db will do
a lot of queries just for a few enumerables.

so each of us have to decide whether to use another 'enumerate table' or
nor

tom

Billee D. wrote:
>
>> fixed values. for example
>> 2. Keep the field internally as a code like {:california =>
>> please suggest ??
>>
>> vipin
>

--
===============================================================================
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com  www.maxwellrender.cz  www.lightgems.cz
===============================================================================
Ff834f7363c1dc9c48b525fc48c67912?d=identicon&s=25 Vipin (Guest)
on 2009-05-08 09:11
(Received via mailing list)
in database only ...i am going to store both string /id in database
only
Ff834f7363c1dc9c48b525fc48c67912?d=identicon&s=25 Vipin (Guest)
on 2009-05-08 09:11
(Received via mailing list)
On May 7, 5:47 pm, Tom Z Meinlschmidt <to...@meinlschmidt.org> wrote:
> country_id column)
> def enum_list_to_text
> > staus => {Single, Married, Divorced }
> > 01, :albama => 02, washington => 03 ....} but while showing the state
>
> > vipin
>
> --
> ===============================================================================
> Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
>
> www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
> ===============================================================================


Thanks ! i will try this.
Ff834f7363c1dc9c48b525fc48c67912?d=identicon&s=25 Vipin (Guest)
on 2009-05-08 09:13
(Received via mailing list)
On May 7, 6:24 pm, Colin Law <clan...@googlemail.com> wrote:
>
> that I understand, I would suggest some variant of option 2, store an id in
> each record referencing a country and determine the string by an enumeration
> or table lookup later.  Worrying too much about computing time during
> development is a mugs game, an application always ends up with most of its
> computing time in an area you do not expect.  Worry about optimisation later
> when (or more likely if) a particular area becomes a problem.
>
> Colin
>
>

Colin,
yes i guess 2nd variant will be better.
thanks
Ff834f7363c1dc9c48b525fc48c67912?d=identicon&s=25 Vipin (Guest)
on 2009-05-08 09:14
(Received via mailing list)
On May 7, 8:09 pm, "Billee D." <william.dod...@gmail.com> wrote:
>
> > in a database table if there is a field which has a certain set of
>
>
> > please suggest ??
>
> > vipin

But Bilee,
if we use another table and do the mapping through foreign key won;t
it be even poorer as we will be making 2 SQL queries to access the
same record. which we are doing in one SQL query in above two
methods.

But i ll certainly check the link provided.

vipin
208af8ce969fdf68f866abd96485c6fe?d=identicon&s=25 mift99 (Guest)
on 2009-05-08 16:35
(Received via mailing list)
I would also use a small join table with your sets ... so your users
can add properties on the fly.
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-05-08 18:20
(Received via mailing list)
The question isn't really a performance issue, but rather one of
customization. If users are likely to need to add values to an
enumeration, (example: category for a blog post), then a lookup table
is a good idea. If the values are substantially unlikely to change
(see your examples - marriage status and state) then there's not a lot
to be gained by complicating the DB.

--Matt Jones
15f2cce3b95bfd989f3700f595000d6c?d=identicon&s=25 lukasw (Guest)
on 2009-05-12 19:07
(Received via mailing list)
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
This topic is locked and can not be replied to.