views:

20

answers:

3

Hello,

In my database for a column called 'how_get' I can have a value of 1, 2, 3 and so on... so in my view to display the data I use:

<%=h @offer.how_get %>

and I get the value displayed 1, 2.... but what I would like ti to have a hash-table with values like this:

@how_d = {'By train' => '1', 'By car' => '2'} 

so instead of '1' it would display 'by train' and so on. This is an just an example and I will have many possible values in my hash-table, so I don't want to use the if statement.

Any ideas?

A: 

You can flip that hash around:

@how_d = {1 => 'By train', 2 => 'By car'}

Then in your view:

<%=h @how_d[@offer.how_get] %>
Lolindrath
A: 

Override how_get method like this

def how_get
  mapping = {'1' => 'By train', '2' =>'By car' } 
  self[:how_get].inject({}) {|h,a| h[mapping[a.to_s]] = a.to_s;h } #{"By train"=>"1", "By car"=>"2"}
end
allenwei
A: 

In model:

def how_get
 mapping = {1=>'By train', 2=>'By car'}
 mapping[self.how_get] #or mapping[self[:how_get]] if first does not work
end

In view:

<%=h @offer.how_get %>
Yuri