开发者

rails csv, array shows no data

I am using csv to export data. I am dynamically populating the fields and in order to do so i have added loop in to csv:

csv_data = CSV.generate do |csv|
  csv << [
  @userHeaders.each do |user|
    "#{user.name}"
  end
  ]
end

but this returns some thing like this: [#<User >, #<User >, #<User >, #<User >]

but when i tried to inspect the value after assigning it to a variable, it displays all the n开发者_如何学Cames that have been passed.

name = "#{user.name}"
puts name.inspect  # (this diaplays all the names)

is there any format to display the data while looping inside csv?

please guide with answer thanks


Try just using the map (aka collect) method of your array:

csv_data = CSV.generate do |csv|
  csv << @userHeaders.map { |user| user.name }
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜