首页 > 解决方案 > 在rails中对字符串类型的键使用转义序列

问题描述

您好,我有一个哈希值,我正在尝试将其合并到一个算法中,该算法最终应该更新我的一个模型中具有特定 ID 的列。在我的调试器中,一切似乎都很好,我得到了正确的数据。我想到的唯一错误是我的哈希有字符串类型的键。有没有办法向这些字符串添加转义序列,这样它们就不会破坏我的代码?

这是哈希

if Rails.env.development?
        loan_sectors = { "Agriculture/Farming" => [8], "Bars/Public Houses" => [1,2,8,9,6], "B&Bs" => [], "Beauty" => [], "Bio Pharma Engineering" => [],
                "Cafes" => [], "Car Sales Industry" => [], "Construction - Commercial" => [127], "Construction - Residential" => [],
                "Consultancy" => [], "Distribution Services" => [],  "Education" => [] , "Engineering"  => [], "Entertainment" => [],
                "Environmental and CleanTech Products and Services" => [] , "Financial Services" => [] , "Garages—Car Repair etc." => [],
                "Health" => [], "Hotels" => [] , "Legal services" => [], "Marketing Services" => [], "Media Services" => [] , "Motor Industry" => [],
                "Manufacturing"  => [],  "Pharmaceuticals" => [], "Recruitment Services" => [], "Restaurants" => [], "Retail Services" => [],
                "Telecoms Industry" => [], "Tourism" => [], "Transport - Import" => [], "Transport - Export" => [],
                "Transport - Internal" => [],"Wholesale" => []}

        loan_sectors.each do |k, v|
          byebug
          sector = LoanSector.where(name: k).first
          v.each do |cid|
            Campaign.find(cid).loan.update_column(:loan_sector_id, sector.id)
          end
        end
      end

标签: ruby-on-railsruby

解决方案


推荐阅读