【发布时间】:2018-07-23 19:34:44
【问题描述】:
我在用 ruby 写入 CSV 文件时遇到问题,无法弄清楚。
> header = "Full Name, Email, Phone Number" csv_file =
> CSV.open("myfile.csv", "wb") do |csv|
> csv << header
> inactive_users_id.each do |inactive_id| #inactive_users_id is just an array on integers, which represent user id
> full_name = User.find(inactive_id).full_name
> email = User.find(inactive_id).email
> phone_num = User.find(inactive_id).phone_number
> desired_info = Array.new
> desired_info.push(full_name)
> desired_info.push(email)
> desired_info.push(phone_num)
> csv << desired_info
> end
> end
我确实收到以下错误:
- “全名、电子邮件、电话”的未定义方法 `map' 数字”:字符串
- 如果我删除行 csv 代表 id 和其他 东西不在那里。
可能是什么问题? 谢谢!
【问题讨论】:
-
header = ["Full Name", "Email", "Phone Number"]。CSV需要Array的列。它将处理逗号。 -
我更改了标题,但它甚至没有写入 csv 文件。主要问题是为什么数组 desired_info 没有写入 csv 文件。
-
另请参阅如何使用 Ruby 将列标题写入 csv 文件?:stackoverflow.com/questions/15905985/…
标签: ruby ruby-on-rails-3