code4fun
code4fun

Reputation: 166

How do I match a database ID when pulling data from a database with an ID that needs to match a text string?

If the question isn't clear enough, I'm saving, for example, states in a database with an ID. I can return that ID but then I'm having difficulty turning it into the state name, for instance "California".

The Initial Form Code:

<%= f.select :user_state, options_for_select(states, f.object.user_state), {}, { :class => 'form-control' } %>

California is ID 5 which is being saved and output correctly using the following code:

<%= current_user.user_state %>

However, I need it to display "California" instead of "5".

UPDATE: Model doesn't have anything with this...

Schema For Users:

create_table "users", force: :cascade do |t|
t.string   "email",                  default: "",    null: false
t.string   "encrypted_password",     default: "",    null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,     null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at",                             null: false
t.datetime "updated_at",                             null: false
t.boolean  "agreement_termspp",      default: false
t.string   "full_name"
t.string   "phone_number"
t.text     "extra_details"
t.string   "user_address"
t.string   "user_city"
t.string   "user_state"
t.string   "user_zipcode"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end


UPDATE 2: I have a users_helper.rb file with the following:

module UsersHelper
    def states
        [
            ["Select A State", "na1"],
        ["----", "na2"],
        ["Alabama", "1"],
        ["Alaska", "2"],
        ["Arizona", "3"],
        ["Arkansas", "4"],
        ["California", "5"],
        ["Colorado", "6"],
        ["Connecticut", "7"],
        ["Delaware", "8"],
        ["District Of Columbia", "9"],
        ["Florida", "10"],
        ["Georgia", "11"],
        ["Hawaii", "12"],
        ["Idaho", "13"],
        ["Illinois", "14"],
        ["Indiana", "15"],
        ["Iowa", "16"],
        ["Kansas", "17"],
        ["Kentucky", "18"],
        ["Louisiana", "19"],
        ["Maine", "20"],
        ["Maryland", "21"],
        ["Massachusetts", "22"],
        ["Michigan", "23"],
        ["Minnesota", "24"],
        ["Mississippi", "25"],
        ["Missouri", "26"],
        ["Montana", "27"],
        ["Nebraska", "28"],
        ["Nevada", "29"],
        ["New Hampshire", "30"],
        ["New Jersey", "31"],
        ["New Mexico", "32"],
        ["New York", "33"],
        ["North Carolina", "34"],
        ["North Dakota", "35"],
        ["Ohio", "36"],
        ["Oklahoma", "37"],
        ["Oregon", "38"],
        ["Pennsylvania", "39"],
        ["Rhode Island", "40"],
        ["South Carolina", "41"],
        ["South Dakota", "42"],
        ["Tennessee", "43"],
        ["Texas", "44"],
        ["Utah", "45"],
        ["Vermont", "46"],
        ["Virginia", "47"],
        ["Washington", "48"],
        ["West Virginia", "49"],
        ["Wisconsin", "50"],
        ["Wyoming", "51"],
        ["---- ", "na3"],
        ["American Samoa", "100"],
        ["Guam", "101"],
        ["Northern Mariana Islands", "102"],
        ["Puerto Rico", "103"],
        ["United States Minor Outlying Islands", "104"],
        ["Virgin Islands", "105"]
    ]
end

Upvotes: 0

Views: 59

Answers (2)

Md. Farhan Memon
Md. Farhan Memon

Reputation: 6121

In your user.rb, you can define a method say

def user_state_name
  State.find_by_id(user_state).try(:name)
end

then you can call it as

current_user.user_state_name

UPDATE

define another method in your helper to convert it into hash and return name of state for id

def state_name(id)
  states.to_h.invert[id.to_s]
end

In your view,

state_name(current_user.user_state)

A small suggestion though, since they are constants, let them be..

STATES = [ #whole state array
  ].freeze

STATES_HASH = STATES.to_h.invert.freeze

def states
  STATES
end

def state_name(id)
  STATES_HASH[id.to_s]
end

Upvotes: 1

John Hayes-Reed
John Hayes-Reed

Reputation: 1428

find the position of the state you want in your states helper array like:

idx = states.index { |state| state.second == current_user.user_state }

and then grab it

states[idx]

which can be put into one line like:

states[states.index { |state| state.second == current_user.user_state }]

Upvotes: 1

Related Questions