I have a Usuario
template and for it I have two constants that define a type for it. To pass this information to a select
I have a type method that returns a Array
. Ex:
#models/Usuario.rb
class Usuario < ActiveRecord::Base
def tipos
[["Tipo 1",1], ["Tipo 2",2]]
end
end
#views/usuarios/_form.html.erb
<div class="field">
<%= f.label :tipo %><br>
<%= f.select :tipo,@usuario.tipos, :class=>"form-control" %>
</div>
So, in my index
will appear 1 and 2 respectively to their types. Obviously I would like Type 1 and Type 2 to be displayed in each case .
Currently as I'm doing this:
#models/Usuario.rb
class Usuario < ActiveRecord::Base
def tipos
[["Tipo 1",1], ["Tipo 2",2]]
end
def tipo_tos
#inverte chave, valor, converte em Hash, pega o nome do tipo
Hash[tipos.map { |t| [t[1],t[0]] }][self.tipo]
end
end
Some more efficient way to do this?