here is something I encounter very often in my project, and I still
don’t know what the real “right” way to solve it:
I have a set of custom values. May it be the countries for a country
select box, a set of games and their shortcuts on a gaming site etc.
Those values are only used very rarely (at the registration form, in
the info panel of games) and there is no real logic behind them; they
are just there to be fetched and rarely to be updated (which would be
done via console).
I see 2 ways of doing this:
You create an array, make it a constant, and change it in the code
somewhere when there are changes to be made
You create a separate model for the data (migration, model file,
table) and insert/destroy records when you need to.
What is the right one?
How hard does the extra model go on the database? And more
importantly, what is the appropriate style of coding this?
If you need let’s say the countries for a select statement I would
just create this in a migrations/country model that loads the data
into an array on startup. This will then be cached in production and
only loaded once. I use something like this for one project:
class State < ActiveRecord::Base
NAMES_ABBREVIATIONS = self.find(:all, :order => :name).map do |s|
[s.name, s.abbreviation]
end
end
If you need let’s say the countries for a select statement I would
just create this in a migrations/country model that loads the data
into an array on startup.
This is extremely unreliable, not to mention conceptually wrong.
Migrations do not necessarily get run for new installations, and anyway
constant data like this has nothing to do with DB migration. Use
seed_fu or similar.