Created
December 14, 2012 16:02
-
-
Save bogdanRada/4286498 to your computer and use it in GitHub Desktop.
Quickly dump ActiveRecord to CSV (Ruby 1.9)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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