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
精彩评论