从 ID 需要匹配文本字符串的数据库中提取数据时,如何匹配数据库 ID?
How do I match a database ID when pulling data from a database with an ID that needs to match a text string?
如果问题不够清楚,例如,我正在使用 ID 将状态保存在数据库中。我可以 return 这个 ID,但是我很难把它变成州名,例如 "California".
初始表单代码:
<%= f.select :user_state, options_for_select(states, f.object.user_state), {}, { :class => 'form-control' } %>
California 是 ID 5,正在使用以下代码正确保存和输出:
<%= current_user.user_state %>
但是,我需要它显示 "California" 而不是“5”。
更新:
模型与此无关...
用户架构:
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
更新 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
在你的user.rb
中,你可以定义一个方法say
def user_state_name
State.find_by_id(user_state).try(:name)
end
那么你可以称它为
current_user.user_state_name
更新
在你的助手中定义另一个方法将其转换为散列和return id 的状态名称
def state_name(id)
states.to_h.invert[id.to_s]
end
在您看来,
state_name(current_user.user_state)
一个小建议,因为它们是常量,所以让它们成为..
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
在您的状态助手数组中找到您想要的状态的位置,例如:
idx = states.index { |state| state.second == current_user.user_state }
再抢
states[idx]
可以像这样放在一行中:
states[states.index { |state| state.second == current_user.user_state }]
如果问题不够清楚,例如,我正在使用 ID 将状态保存在数据库中。我可以 return 这个 ID,但是我很难把它变成州名,例如 "California".
初始表单代码:
<%= f.select :user_state, options_for_select(states, f.object.user_state), {}, { :class => 'form-control' } %>
California 是 ID 5,正在使用以下代码正确保存和输出:
<%= current_user.user_state %>
但是,我需要它显示 "California" 而不是“5”。
更新: 模型与此无关...
用户架构:
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
更新 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
在你的user.rb
中,你可以定义一个方法say
def user_state_name
State.find_by_id(user_state).try(:name)
end
那么你可以称它为
current_user.user_state_name
更新
在你的助手中定义另一个方法将其转换为散列和return id 的状态名称
def state_name(id)
states.to_h.invert[id.to_s]
end
在您看来,
state_name(current_user.user_state)
一个小建议,因为它们是常量,所以让它们成为..
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
在您的状态助手数组中找到您想要的状态的位置,例如:
idx = states.index { |state| state.second == current_user.user_state }
再抢
states[idx]
可以像这样放在一行中:
states[states.index { |state| state.second == current_user.user_state }]