csv导入数据库时​​,未正确插入数据到datetime列

时间:2015-12-14 12:51:26

标签: ruby-on-rails ruby csv mysql2

我在rails应用程序上创建了一个ruby。将日期导入available_on列时,日期未正确插入数据库。当我使用csv文件导入数据时,它会发生更改。

def import
  require 'csv'
  file = params[:file]
  CSV.foreach(file.path, headers: true) do |row|
    @prod = Spree::Product.new()
    @prod.name = row["name"]
    @prod.shipping_category_id = row["shipping_category_id"]
    @prod.description = row["description"]
    @prod.available_on = row["available_on"]
    @prod.meta_description = row["meta_description"]
    @prod.meta_keywords = row["meta_keywords"]
    @prod.tax_category_id = row["tax_category_id"]
    @prod.shipping_category_id = row["shipping_category_id"]
    @prod.promotionable = row["promotionable"]
    @prod.meta_title = row["meta_title"]
    @prod.featured = row["featured"]
    @prod.supplier_id = row["supplier_id"]
    @prod.master.price = row["master_price"]
    @prod.master.cost_price = row["cost_price"]
    @prod.master.depth = row["depth"]
    @prod.master.height = row["height"]
    @prod.master.width = row["width"]
    @prod.master.weight = row["weight"]
    @prod.master.sku = row["sku"]
    @prod.master.tax_category_id = row["tax_category_id"]
    @prod.save!
  end 
end

我的数据库表就像:

| id                   | int(11)     
| name                 | varchar(255)
| description          | text        
| available_on         | datetime    
| deleted_at           | datetime    
| slug                 | varchar(255)
| meta_description     | text        
| meta_keywords        | varchar(255)
| tax_category_id      | int(11)     
| shipping_category_id | int(11)     
| created_at           | datetime    
| updated_at           | datetime    
| promotionable        | tinyint(1)  
| meta_title           | varchar(255)
| featured             | tinyint(1)  
| supplier_id          | int(11)    

我知道这是由于available_on的数据类型。 我在available_on中插入日期就像' 2015-10-10' 任何人都可以告诉如何在导入csv文件时从数据库中删除这种不一致性

2 个答案:

答案 0 :(得分:1)

使用:converters选项告诉Ruby自动转换日期字段:

require 'csv'

def import
  file = params[:file]

  CSV.foreach(file.path, headers: true, converters: :date) do |row|
    # ...
  end 
end

P.S。我可以建议稍微清理一下吗?

PRODUCT_ATTR_NAMES = %w[ name             shipping_category_id  description
                         available_on     meta_description      meta_keywords
                         tax_category_id  promotionable         meta_title
                         featured         supplier_id ]

MASTER_ATTR_NAMES = %w[ master_price  cost_price  depth  height
                        width         weight      sku    tax_category_id ]

def import
  require 'csv'
  file = params[:file]

  CSV.foreach(file.path, headers: true, converters: :date) do |row|
    row = row.to_hash
    product_attrs = row.slice(*PRODUCT_ATTR_NAMES)
    master_attrs = row.slice(*MASTER_ATTR_NAMES)

    @prod = Spree::Product.create!(product_attrs) do |product|
      product.master.assign_attributes(master_attrs)
    end
  end 
end

答案 1 :(得分:0)

您应该将值解析为类Time的实例:

require 'time'

@prod.available_on = Time.parse(row["available_on"])

您很可能也希望在其他列上进行某种类型转换。应包含整数值的那些,例如:

@prod.tax_category_id = Integer(row["tax_category_id"])

同样适用于其他非字符串值。