[英]Ruby-on-Rails Rake import unexpected tASSOC
I'm trying to load data from a csv as a rake task with the following code: 我正在尝试使用以下代码从csv加载数据作为rake任务:
require 'csv'
desc "Import Users from csv file"
task :import_users => [:environment] do
file = "db/users.csv"
CSV.foreach(file, :headers => true) do |row|
User.create {
:name => row[1],
:email => row[2],
:password => row[3],
:password_confirmation => row[4],
:admin => row[5]
}
end
end
and the CSV: 和CSV:
name,email,password,password_confirmation,admin
john smith,js@mail.com,password,password,TRUE
im getting the a unexpected tASSOC error for each attribute. 即时通讯收到每个属性意外的tASSOC错误。
If you put brackets around your hash in the create method it will work fine: 如果在create方法中将括号放在哈希表中,它将可以正常工作:
User.create({
:name => row[1],
:email => row[2],
:password => row[3],
:password_confirmation => row[4],
:admin => row[5]
})
Basically ruby doesn't understand that this is a method call with a hash as the parameter. 基本上,ruby无法理解这是一个以哈希作为参数的方法调用。 By putting the brackets there you make it explicit that that is what you mean. 通过在其中放置括号,您可以清楚地表明这是您的意思。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.