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 .
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)
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
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)
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.
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):
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
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.
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.
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: