开发者

rails csv, array shows no data

开发者 https://www.devze.com 2023-03-11 03:00 出处:网络
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:

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

精彩评论

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

关注公众号