Using store values from drop down list

Hi,

I was trying to understand stored values vs display values.

model code

CLUB_TYPES = [

Displayed stored in db

[ “Public” , “pub” ],
[ “Village” , “vil” ],
[ “Private” , “pvt” ]
]

validates_inclusion_of :club_type, :in =>
CLUB_TYPES.map {|disp, value| value}

#view code

<%= f.select :club_type, Club::CLUB_TYPES, :prompt => “Select a Club
Type” %>

This works fine for getting data into the database.

My question: Is there a helper to get the display value back from the
database? If not, could somebody show me how to correct the following
to get the display value rather than the stored value.

Club Type:
<%= @club.club_type %>

Thanks

On 10 March 2010 14:41, Andy J. [email protected] wrote:

I would change that as follows:
CLUB_TYPES = {
“pub” => “Public”,
“vil” => “Village”,
“pvt” => “Private”
}

…as long as there’s no need for clubtypes to be displayed in any
particular order (arrays are returned in order, hashs aren’t
necessarily)

For the minuscule amount of extra effort involved, create a model for
club types, seed it, and give it a “position” column if you want to
manage the order (or just put an :order clause on your default
finder).

Then use ClubType.all or named scopes for more functionality …

Your clubs can change their club_type text column into a club_type_id
integer and add a belongs_to relationship, allowing you to do nice
easy queries, leverage Rails’ functionality, and get rid of those
“magic” array/hash variables.

Sometimes things that seem like shortcuts (like using an array for
some “static” data) can quickly turn into dead ends. The framework
does all the hard work for you, so you might as well use it.

Thanks for the advice. Changing to a hash did what I wanted it to.

I can see the advantage of generalizing this in a model. Thanks for
the suggestion.

model code

CLUB_TYPES = [

Displayed stored in db

[ “Public” , “pub” ],
[ “Village” , “vil” ],
[ “Private” , “pvt” ]
]

I would change that as follows:

CLUB_TYPES = {
“pub” => “Public”,
“vil” => “Village”,
“pvt” => “Private”
}

validates_inclusion_of :club_type, :in =>
CLUB_TYPES.map {|disp, value| value}

So that becomes:

validates_inclusion_of :club_type, :in => CLUB_TYPES.keys

#view code

<%= f.select :club_type, Club::CLUB_TYPES, :prompt => “Select a Club
Type” %>

I think when using a hash you need to invert it, something like this:

<%= f.select :club_type, Club::CLUB_TYPES.invert, :prompt => “Select a
Club
Type” %>

You may need to play with that a little bit… I can’t remember and
don’t
have time to test it at the second…

This works fine for getting data into the database.

My question: Is there a helper to get the display value back from the
database? If not, could somebody show me how to correct the following
to get the display value rather than the stored value.

Club Type:

<%= @club.club_type %>

<%= Club::CLUB_TYPES[@club.club_type] %>

(or more likely a method on the model call club_type_description or
something similar that does the same thing).

Cheers,

Andy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs