我在 StackOverflow 上查看了一些问题,但没有找到我的案例(我觉得这很奇怪,因为我的目标似乎很常见)。
我有两个模型:Products关联Categories如下:
- 产品belongs_to :category
- 类别has_many :products
产品有栏category_id
CVS 文件具有以下列:
- 产品名称,
- 分类名称,
- 产品价格
我到底如何category_name从文件中获取并插入  category_id到 Products 表中?
我有来自 Rails-Cast 的以下代码:
  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      Response.create! row.to_hash
    end
  end
谢谢!