mardi 21 février 2017

map concern hash keys with the table column

I created a concern in model

module Employee
  extend ActiveSupport::Concern

  included do
    def job
      job_hash = {
        '0': 'A',
        '1': 'B',
        '2': 'C'
       }.freeze
       job_hash[:"#{@job}"]
  end
end

I have model

class Company < ActiveRecord::Base
  self.table_name = 'Jobs'
  include Employee
end

Jobs table has column type that has values 0, 1, 2 How can i map the values from hash (0,1,2) in concern with column type and return the string value associated with it? so if the user type is 1 then return B

Thanks

Aucun commentaire:

Enregistrer un commentaire