2

I try to made a scheduled daily csv mail. I put inside my Importer class: " include Roo" then I put 2 functions:

def self.daily_mail
  Company.find_each do |c|
    export_to_csv(c)
  end
end

def self.export_to_csv(company)
  @opportunities = company.opportunities
  csv_data = CSV.generate do |csv|
    csv <<  ["Opportunity Id", "Created At", "Request Id", "Champion Id", "Referral_Id"]
    @opportunities.all.each do |opportunity|
      csv << [opportunity.id, opportunity.created_at, opportunity.request_id, opportunity.champion_id, opportunity.referral_id]
    end
  end
end

And in the scheduler class I coded:

task :export => :environment do
  puts "Exporting..."
    Importer.daily_mail
  puts "done."
end

However, when I tried to run in my terminal:

rake export

I got:

rake aborted!
NoMethodError: undefined method `generate' for Roo::CSV:Class

Can someone help me pleas?

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
miss_M
  • 129
  • 2
  • 11

1 Answers1

3

I'm not sure why you need Roo here, but only for exporting csv file, the standard CSV library is enough. CSV.generate is a method from standard library, so to use this, you have to add require 'csv' to Importer file.

You also need to remove "include Roo" to avoid conflict. But if you still want to work with Roo, please replace CSV.generate by ::CSV.generate.

Van Huy
  • 1,607
  • 1
  • 13
  • 16