Models vs constant arrays (Rails way)

Hey guys,

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:

  1. You create an array, make it a constant, and change it in the code
    somewhere when there are changes to be made
  2. 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?

Thanks,
J.S.

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

<%= address_form.select(:state, State::NAMES_ABBREVIATIONS, :prompt =>
‘select’, :label => “State”, :required => true) %>

This gives only one hit to the database on startup.

Domain tables are your friends.

On 4 Okt., 05:40, pharrington [email protected] wrote:

Domain tables are your friends.

huh?
google doesn’t give me anything intelligent there

2009/10/6 Jonas S. [email protected]:

On 4 Okt., 05:40, pharrington [email protected] wrote:

Domain tables are your friends.

huh?
google doesn’t give me anything intelligent there

He just means the same thing that heimdull had already suggested, I
think.

Colin

heimdull wrote:

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.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]