导入 CSV 时如何覆盖模型中的 created_at 字段? [英] How to override created_at field in model when importing a CSV?

查看:41
本文介绍了导入 CSV 时如何覆盖模型中的 created_at 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个包含等待名单注册的 CSV 文件导入到我的数据库中,并带有之前创建的日期,如何在导入它们的同时保持它们的初始日期,而不是让它们都显示相同的导入日期?

I'm importing a CSV full of waitlist signups into my database with previous created dates, how can I import them while keeping their initial dates vs. having them all show the same date of importing?

我收到错误消息:Rails 无法为 id、created_at 批量分配受保护的属性

代码:

 csv_file = params[:csv][:file].read
    csv = CSV.parse(csv_file, :headers => false) 
    csv.each do |row|
       Model.create!(:email => row[0], :created_at => row[1])    
    end    

推荐答案

您需要将所需的列添加到attr_accessible

You need to add the desired column to the attr_accessible

class Tutorial < ActiveRecord::Base
  attr_accessible :created_at
end

这篇关于导入 CSV 时如何覆盖模型中的 created_at 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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