从具有需要匹配文本字符串的ID的数据库中提取数据时,如何匹配数据库ID?

时间:2023-01-14 19:26:31

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".

如果问题不够明确,我会保存,例如,在具有ID的数据库中的状态。我可以返回该ID但是我很难将其转换为州名,例如“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:

California是ID 5,使用以下代码正确保存和输出:

<%= current_user.user_state %>

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

但是,我需要它来显示“California”而不是“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:

更新2:我有一个users_helper.rb文件,其中包含以下内容:

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

2 个解决方案

#1


1  

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

在您的user.rb中,您可以定义一个方法

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

在helper中定义另一个方法,将其转换为hash,并返回id的state name

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

#2


1  

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 }]

#1


1  

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

在您的user.rb中,您可以定义一个方法

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

在helper中定义另一个方法,将其转换为hash,并返回id的state name

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

#2


1  

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 }]