Mark
Mark

Reputation: 6455

Rails - Passing an Active Record relation into a CSV

I'm trying to filter through all venues, and add any without an external_join_url to the CSV I've created.

require 'csv'
namespace :create_dump do
  desc "Erase and fill database"
  task :venue => :environment do

    @venues = Venue.where(external_join_url: nil)
    CSV.open("venues_without_join_url.csv", "w") do |csv|
      csv << ["Venue Title"]
      @venues.each do |venue|
        csv << venue.title
      end
    end
  end
end

When I attempt to do so, I get the error:

NoMethodError: undefined method `map' for "5 Pancras Square":String

I get this means I'm trying to map a string, but can't see what part I'm creating the string. I've tried different ways of assigning @venues (creating an array and shovelling into it) to no avail.

Upvotes: 0

Views: 220

Answers (1)

user1616238
user1616238

Reputation:

csv << expects an array of strings that's meant to be the fields of a row in the csv file

require 'csv'
namespace :create_dump do
  desc 'Erase and fill database'
  task venue: :environment do
    @venues = Venue.where(external_join_url: nil)
    CSV.open('venues_without_join_url.csv', 'w') do |csv|
      csv << ['Venue Title']
      @venues.each do |venue|
        csv << [venue.title]
      end
    end
  end
end

Upvotes: 1

Related Questions