Module: ActiveRecordToCSV

Defined in:
lib/active_record_to_csv.rb

Instance Method Summary collapse

Instance Method Details

#to_csvObject

Return full CSV content with headers as string. Defined as class method which will have chained scopes applied.


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_record_to_csv.rb', line 7

def to_csv
  csv_columns = column_names - %w{id created_at updated_at}
  header_row = csv_columns.to_csv
  records_rows = all.map do |record|
    csv_columns.map do |column|
      value = record.send(column)
      value = value.to_csv if value.respond_to?(:to_csv)
      value
    end.to_csv
  end.join
  header_row + records_rows
end