Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bogdanRada/4286498 to your computer and use it in GitHub Desktop.
Save bogdanRada/4286498 to your computer and use it in GitHub Desktop.
Quickly dump ActiveRecord to CSV (Ruby 1.9)
def dump_model(klass)
File.open("#{klass.to_s.underscore.pluralize}.csv", 'w+') do |f|
columns = klass.columns.collect(&:name)
output = CSV.generate do |csv|
csv << columns.map { |column| klass.human_attribute_name(column) }
klass.all.each do |item|
csv << columns.collect { |column| item.send(column) }
end
end
f.puts output
end
end
dump_model(Posts) # => look in RAILS_ROOT/posts.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment