Guest User

Untitled

a guest
May 20th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.12 KB | None | 0 0
  1. def export_to_csv
  2.  
  3.       @project = Project.find(params[:id])
  4.         respond_to do |format|
  5.           format.csv do
  6.           @csv_string = FasterCSV.generate(:col_sep => ";") do |csv|    # calling method from gem FasterCSV
  7.  
  8.             # header row for Project
  9.            csv << ["id", "name", "description", "budget", "added_on", "current_status"]
  10.             # data rows for Project
  11.             csv << [@project.id, @project.name, @project.description,
  12.                     @project.budget, @project.added_on, @project.current_status]
  13.             csv << []         # empty line
  14.  
  15.             # header rows for task that belongs to Project
  16.             csv << ["id", "priority", "project.id", "title", "description",
  17.                     "assigned_user", "completed", "created_at", "updated_at"]
  18.              @project.tasks.each do |task|
  19.  
  20.               # data rows for task Project
  21.               csv << [task.id, task.priority, task.project.id, task.title, task.description,
  22.                       task.assigned_user, task.completed, task.created_at, task.updated_at]
  23.  
  24.              end
  25.              end
  26.         end
  27.   end
Add Comment
Please, Sign In to add comment