Aasish Sharma Aasish Sharma - 3 months ago 8
Ruby Question

How to send Hyperlink(Task URL) in CSV Export

I want show Task_Url in the CSV file, I have used this query for CSV -

user_trackers = TaskTimeTracker.joins(:task).where("(task_time_trackers.created_at BETWEEN ? AND ?)",Date.current.beginning_of_month, Date.current.end_of_month )


@trackers = user_trackers

This is the CSV method I have used -

def export_csv
columns = %w(Date Task Task_URL TimeSpent Log)
CSV.generate do |csv|
csv << columns
@trackers.each do |t|
csv << [t.created_at,
t.task.name,

t.time_spent,
t.description ]
end
end
end


This is the Task Model I have used.

class Task < ActiveRecord::Base
TASK_TYPES = { bug: 'Bug', enhancement: 'Enhancement', feature: 'Feature'}
belongs_to :project
belongs_to :user
has_many :task_time_trackers, dependent: :nullify
validates :task_type,presence: true
end


Task_Time_Tracker Model

class TaskTimeTracker < ActiveRecord::Base
belongs_to :task
belongs_to :user
validates :description,presence:true
validates :time_spent,presence:true
end


Schema Info of Task_Time_Tracker

# == Schema Information
#
# Table name: task_time_trackers
#
# id :integer not null, primary key
# description :text(65535)
# time_spent :float(24)
# created_at :datetime not null
# updated_at :datetime not null
# task_id :integer
# user_id :integer


Project Model

class Project < ActiveRecord::Base
has_many :tasks, dependent: :destroy
has_and_belongs_to_many :users
end

Answer

This method is implemented in a controller or model?

Test this:

def export_csv
  columns = %w(Date Task Task_URL TimeSpent Log)
  CSV.generate do |csv|
    csv << columns
    @trackers.each do |t|
      csv << [t.created_at,
                t.task.name,
                task_url(t.task),
                t.time_spent,
                t.description ]
    end
  end
end