ActiveRecord.to_csv

Description

A simple ActiveRecord::Base to_csv() class method that preserves scopes. to_csv() returns the entire contents including the header ready to be written to file.

Usage

# Assuming a Movie model with title and director_id columns.
Movie.to_csv
# would return:
title,director_id
title,director_id
Black Swan,0
Inception,1
The Fighter,2
The King's Speech,3
The Kids Are All Right,4

Movie.bad.to_csv
# would return:
title,director_id
The Kids Are All Right,4

Note that #to_csv is called like a scope or query. The following will NOT give you the same results:

Movie.all.to_csv

This will use Ruby’s Array#to_csv method.

TODO

Options to specify columns to be included (currently, id and timestamp columns are excluded).

Compatibility

Tested with ActiveRecord v3.0.4

gem-testers.org/gems/active_record_to_csv