Search code examples
ruby-on-railsruby-on-rails-3export-to-excelrails-console

Ruby on Rails console export table to excel


Ruby on Rails 3

I have a table that is not shown on my application. I want to export the table to excel from the console.

Is there way to export a database table to an excel file from the console?

Thank you

Here is what I have so far:

require 'csv'

file = "#{Rails.root}/public/survey.csv"

userrefs = Survey.all

CSV.open( file, 'w' ) do |writer|
 userrefs.each do |ur|
   writer << ur.attributes.values_at(*column_names)
 end
end

When I enter require 'csv' it returns false. How do you make it true? Also, the *column_names is undefined.


Solution

  • As mentioned in the comments an easy approach is using format.xls function to render an excel file from the console. Ryan Bates video covers Excel outputs extensively.