Ruby on Rails console export table to excel - ruby-on-rails

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.

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.

You can connect to the database table using the existing driver or, if you prefer a more high-level API, you can create an ActiveRecord model or use the Sequel gem.
Once connected, simply use the Ruby CSV library (it's in the standard library, so no additional library is required) to dump the content into a CSV file.
CSV files can be easily read from Excel.
PS. Just to use the appropriate words, that is not a Rails table. It's a database table.

Another interesting approach could be using the activeadmin gem, it's easy to install and allow you to export your tables to csv.
http://www.activeadmin.info/

Related

Ruby on Rails - Roo gem | Issue opening active record attachment

Status:
I have a simple new Ruby On Rails App
I need to import some data from MS Excel in .xlsx format
I programmed an uploading to ActiveRecord as attachment via an attribute called 'excel'
I found a gem called Roo which should do the opening of the attachment
...via: Roo::Excelx.open() command
Then executes the accessing part of the file
Issue:
Roo::Excel.open() doesnt work for:
Roo::Excel.open(excel)
Roo::Excel.open(excel.attachment)
What is the correct command to execute to open the ActiveRecord::Attachment?
how about do this?
ModelName.excel.open do |file|
xlsx = Roo::Spreadsheet.open(file)
end
Actually I got an answer outside of Stackoverflow which was this:
Roo::Spreadsheet.open(ActiveStorage::Blob.service.path_for(excel.key), extension: 'xlsx')
I loaded one row from the Excel so must change code but opening it seemed to have worked! :-)

How to control RoR I18n texts stored in database?

I'm using the I18n Gem from Sven Fuchs in my Ruby on Rails 3.2 Application and while the gem works great I came across a situation, which I don't know the solution to:
I have a seed file, which contains the basic translation for my MVC's and is seeded, when I install my application on a new machine. The problem is that when one of these translations changes, I have to go to my seed file, edit it, delete in the database and reseed it. Which is problem not the best way to do this.
Furthermore, my application can create complete MVC's on the fly, which of course need translations as well. These translations get only stored in the database. But it would be nice to store them in a real file, keep them under version control and import or export them if I need to.
So, basically what I'm looking for, is an intelligent connection between the translations in my database and the ones in my files. So I can populate one from the other or vica verca and keep them in sync.
And also I looked at solutions like Globalize3 or localeapp, but they don't seem to fit.
Summarized, what I have is:
The I18n Gem from Sven Fuchs with a Backend I created myself
A Seed File which changes sometimes and has to be edited manually but seeds the basic translations
A database which contains translations that are created on the fly and are not under version control, nor stored in some file
What I want:
A sync between translations in my seed file and my database
A way to put my translations under version control
I'm sure I can't be the only one who needs this...
Thanks in regards!
Here is how I solved a problem closer to the question asked:
task :task_name => [:environment] do
file = "db/file_name.txt"
counter = 0
CSV.foreach(file, :headers => true, :col_sep => "^", :quote_char => "~") do |row|
identifier = row[0].to_i
model_name = ModelName.find_or_create_by_identifier(identifier)
I18n.locale = row[1]
model_name.name = row[3]
model_name.save!
end
end
Note that identifier needs to be a unique identifier that doesn't change and exists in the file and in the database. In this example, the columns are separated by "^" and quores are "~"
As #tigrish said in the comments, it is not a good idea to insert in the file and in the database, so it is important to restrict this.
These links may also help:
http://railscasts.com/episodes/396-importing-csv-and-excel
http://jasonseifer.com/2010/04/06/rake-tutorial
As the question is a little old, I hope it can help somebody else.

How to modify XML and XLSX output from Active Admin?

I've been trying to modify the output (export) of model information from Active Admin for a rails application. I can modify the CSV output using the
csv do
..
end
Is there a method to modify the output of XML or XLXS in a similar way?
ActiveAdmin does not provide tools for customizing xml/json output like it does for csv.
So there are couple options you can do:
Rewrite to_xml method for you model
Create .xml.erb template. For example if you have active admin for User, then your template should be in app/views/admin/users/index.xml.erb

importing excel to sqlite in rails application

I am making an application in Ruby on Rails which will ask users multiple choice questions. I want to upload questions to the database from an excel file. How can I do it?
Save the Excel spreadsheet as a CSV file then use a CSV parser, perhaps in a rake file:
In lib/taks/import.rake:
require 'fastercsv'
namespace :import => :environment do
task :questions do
FasterCSV.foreach("path/to/file.csv") do |row|
q = Question.create(:question=>row[0], etc...)
PossibleAnswer.create(:question=>q, :answer=>row[1], etc....) #providing PossibleAnswer belongs_to Question
end
end
end
Then run "rake import:questions"
You could use the spreadsheet gem to read in the data from an excel file:
http://rubygems.org/gems/spreadsheet
This is most useful if you want to allow users to upload their own excel documents, to import some questions, or if you want users to be able to download questions in excel file format.
If you just want to do a one-off import of some data i would go with Yule's idea and just do it via csv which is much easier to get to grips with.

Plugin to use Ruby on Rails Simple I18n backend with translations overridable in the database?

Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin or gem that allows you to continue using the Simple I18n backend whilst allowing you to optionally specify translations in the database.
Here's why:
I have one Rails app used for many websites. For the example I'll just use 2 websites:
Website 1: Leprechauns R Us
Website 2: Unicorns R Us
Most translations are the same for both websites, but occassionally I want to override a translation. For example, in my en-US.yml file I have the following translation:
view_all: View all
And for most websites this translation is fine, including for website 1 (Leprechauns) where I'm happy to use "View all".
However, for website 2, I'd like to use "View all Unicorns" as the view_all translation and I'd like to specify this in the database. For maintenance reasons I don't want to specify this override in a YAML file.
Many thanks,
Eliot
In the end I opted to take advantage of Rails' I18n::Backend::Simple's ability to process both .yml files and .rb files as locale dictionaries.
Artifacts created:
DB migration to create a translations table with columns: locale, key, text
Translation model to map to the translations table
Class method to_locale_hash on Translation model that returns a locale keyed hash as required by I18n::Backend::Simple.load_rb
A single-line file located at config/translations.rb with the line 'Translation.to_locale_hash'
For the source code see the Spree extension (sorry not in a Rails plugin structure, it will be easy to move over to a plugin should you require it) here:
http://github.com/eliotsykes/spree-i18n-db/tree/master

Resources