Forum: Ruby on Rails Managing options for select boxes

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.
Morten (Guest)
on 2007-02-14 12:45
(Received via mailing list)
When having a select box like:

Select profession:
  * Unemployed
  * Hooligan
  * Canoo

I've on some occasions used a "type" class:

class ProfessionType < BaseType
    @types = {}
    @types[0] =  ProfessionValue.new('Unemployed')
    @types[1] =  ProfessionValue.new('Hooligan')
    @types[2] =  ProfessionValue.new('Canoo')
end

Where BaseType facilitates some convenience methods (allowing for eg.
ProfessionType.HOOLIGAN which returns 1, or returning an array
appropriate for generating the options for a select).

This approach works fine, but doesn't feel quite right. An alternative
approach could be to just hard code the select box and save the string
values directly in the DB. Or one could make a "selectables" table in
the DB and model it like:

Selectable < AR::Base
  #... convenience methods here ...
  def options_for_select
    #...
  end
end

ProfessionSelectable < Selectable
end

But this is probably overdoing it, especially if the options are
fixed.

I'm curious to know how others treat their options for selects - so
what do you do?

Thanks.

Morten
dima (Guest)
on 2007-02-14 14:02
(Received via mailing list)
There is no golden hammer.
For some fix things I use the method in helper and for some dynamic
things usually data is stored in Db somewhere and I think that I have
one using data from yaml file.
So use as you see fit.

dima
This topic is locked and can not be replied to.