Rails 4 CSV导入和设置值为键值 [英] Rails 4 CSV Import and setting a value to a key value

查看:142
本文介绍了Rails 4 CSV导入和设置值为键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完整的Rails n00b,我确定这是一个容易的事情,但我有麻烦。我想要在我的URL中的一个键的值,并将其设置为:我的数据库中的一个记录的category_id,我从csv导入该记录。

I'm a complete Rails n00b and i'm sure this is an easy thing to do but i'm having trouble. I would like to take the value of a key in my URL and set it to the :category_id of a record in my database as i import that record from csv.

i可以通过在我的csv文件中创建一个category_id字段并使用以下代码导入该文件来使其工作。

i can get it to work by creating a category_id field in my csv file and using the following code to import the file

  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      record = Manufacturer.where(:category_id => row[1], :name => row[0] ).first_or_create
      record.save!
    end
  end

但这需要将category_id添加到csv ..我想做的是像

But that requires adding the category_id to the csv.. what I would like to do is something like

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    record = Manufacturer.where(:category_id => @category, :name => row[0] ).first_or_create
    record.save!
  end
end

其中@category在URL中设置。像这样:
... localhost:3000 / manufacturer / import_manufacturer / 2?category_id = 2
这保存我的记录,但将类别ID设置为null - 这是服务器输出:

where @category is set in the URL. Something like: ...localhost:3000/manufacturers/import_manufacturer/2?category_id=2 this saves my record but sets the category id to "null" - this is the server output:

Started POST "/manufacturers/import?category_id=2" for 127.0.0.1 at 2013-07-18 11:19:55 +0200
Processing by ManufacturersController#import as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DuAW1pOnaJieBYN7qEQGortYMC74OtLT6tT/e1dKAiU=", "file"=>#<ActionDispatch::Http::UploadedFile:0x007f835e3cae40 @tempfile=#<File:/var/folders/9j/4hs3_7kx11x3h4gkcrrgkwhc0000gq/T/RackMultipart20130718-23913-k0z3a8>, @original_filename="citroen.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"citroen.csv\"\r\nContent-Type: text/csv\r\n">, "commit"=>"Import", "category_id"=>"2"}
Category Load (0.3ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
Manufacturer Load (0.6ms)  SELECT `manufacturers`.* FROM `manufacturers` WHERE `manufacturers`.`category_id` IS NULL AND `manufacturers`.`name` = 'CITROEN' ORDER BY `manufacturers`.`id` ASC LIMIT 1
 (0.3ms)  BEGIN
SQL (0.5ms)  INSERT INTO `manufacturers` (`created_at`, `name`, `updated_at`) VALUES ('2013-07-18 09:19:55', 'CITROEN', '2013-07-18 09:19:55')
(0.5ms)  COMMIT
(0.2ms)  BEGIN
(0.1ms)  COMMIT
Manufacturer Load (0.6ms)  SELECT `manufacturers`.* FROM `manufacturers` WHERE `manufacturers`.`category_id` IS NULL AND `manufacturers`.`name` = 'CITROEN' ORDER BY `manufacturers`.`id` ASC LIMIT 1
(0.2ms)  BEGIN
(0.1ms)  COMMIT

是否可以像这样将变量传递到csv.foreach循环中?

Is it possible to pass a variable into the csv.foreach loop like this?

提前感谢,对不起,如果我如果类方法 import 被调用,请求一个愚蠢的问题。

Thanks in advance and sorry if I'm asking a stupid question.

推荐答案

在导入控制器操作中,您可以将 params [:category_id] 作为第二个参数。

if the class method import is called in the import controller action, you can pass params[:category_id] as the 2nd parameter.

class ManufacturersController < ApplicationController
  def import
    Manufacturer.import(params[:file], params[:category_id])
  end
end

class Manufacturer < ActiveRecord::Base
  def self.import(file, category_id)
    CSV.foreach(file.path, headers: true) do |row|
      record = Manufacturer.where(
        :category_id => category_id,
        :name => row[0]
      ).first_or_create
    end
  end
end

这篇关于Rails 4 CSV导入和设置值为键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆