I'm working on a project in rails and recently watched this great gorails.com episode on exporting data as CSV (https://gorails.com/episodes/export-to-csv). I can get it to work, but I really want to get the absolute url of each of the "tasks" so in the CSV export it would show in each row as a 4th column the link to the "task" eg: "localhost:3000/tasks/1" "localhost:3000/tasks/2" etc. Something like "task_url" as the column header
I haven't found any resource online that helps with this. Any thoughts on how to do this in the most efficient way? Thank you for the help! Here is the working code:
def self.to_csv
attributes = %w{id task_name task_description}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |task|
csv << attributes.map{ |attr| task.send(attr) }
end
end
end
And I would like to do something like this (emphasis added around task_url):
def self.to_csv
#need to declare what task_url is here
attributes = %w{id task_name task_description **task_url**}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |task|
csv << attributes.map{ |attr| task.send(attr) }
end
end
end