【问题标题】:How do I match a database ID when pulling data from a database with an ID that needs to match a text string?从具有需要匹配文本字符串的 ID 的数据库中提取数据时,如何匹配数据库 ID?
【发布时间】:2017-11-18 17:40:09
【问题描述】:

如果问题不够清楚,我会在数据库中保存状态,例如,使用 ID。我可以返回该 ID,但我很难将其转换为州名,例如“加利福尼亚”。

初始表单代码:

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

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

<%= current_user.user_state %>

但是,我需要它显示“加利福尼亚”而不是“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

【问题讨论】:

  • 发布您的模型和架​​构
  • 你有没有countriesstates 表,california 怎么得到 5?
  • 我有一个助手区...将在上面发布更新...

标签: ruby-on-rails database postgresql rails-postgresql


【解决方案1】:

在你的user.rb,你可以定义一个方法说

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

那么你可以称它为

current_user.user_state_name

更新

在你的帮助器中定义另一个方法将其转换为哈希并返回 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

【讨论】:

  • 我得到一个“#<0x007fd808b5de88>
【解决方案2】:

在你的状态助手数组中找到你想要的状态的位置,比如:

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

然后抓住它

states[idx]

可以放在一行中:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多