importing excel to sqlite in rails application - ruby-on-rails

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.

Related

Creating devise users from a CSV file - Rails - PG

I have been given a CSV file that contains emails and passwords.
My task is to go through this CSV file and create users using the devise gem.
You will probably wonder why I hace been given peoples emails and passwords (I am as well), but I've been told not to worry about it.
My csv file looks like this:
Email,Password,Password_confirmation,,
email1#email.com ,password1,password1,,
email2#email.com ,password2,password2,,
email3#email.com ,password3,password3,,
I wrote the following code in my seeds.rb file:
require 'csv'
CSV.foreach("db/fixtures/users.csv", :col_sep => ",", :headers => true) do |row|
User.create(email: row['email'], password: row['password'], password_confirmation: row['password_confirmation'])
end
I run rails db:seed to create the users, but nothing happens, no error messaged either.
Any support would be welcomed.
First of all, it's a really bad idea to put that file with actual passwords under version control and to import it in seeds.rb. Would be much better to add a rake task for that purpose accepting csv file path as an argument and perform it once in a suitable environment.
About the code, the headers in CSV import are case sensitive, so it would work if you get the row value via row['Email'], row['Password'] etc.
Also, make sure that filling in those fields are enough to save the user, there may be some other required fields in your particular model.

Reading a CSV file stored in Active Storage in Rails 6

I'm using Active Storage to upload CSV files, which are then read to update values in the database. I have set up a basic upload file class to do this;
class VendorFile < ApplicationRecord
has_one_attached :vendor_upload_file
validate :acceptable_file
end
I open the CSV files using the Rails 6 open method like this;
self.vendor_upload_file.open do |file|
CSV.foreach(file) do |row|
do some processing....
end
end
This works great for processing through the whole file. The issue is before processing I'd like to open the file and just read the first line to ensure the file is the correct format. I cannot find a way to open the file and read just the first line if the file is stored in Active Storage. Does anyone know a way to do this?
Thanks!
How about?
headers = self.vendor_upload_file.open(&:first).parse_csv

Rails: Background task for regular feed

I have a new requirement for an existing app and I wanted to ask other developers how to go about developing it. I want to export CSV every 2 days and provide my sales team feeds which they use to bulk import on a designated data storage location, say google drive. they can then check the file and do uploads etc.
I want to run a Heroku scheduler which runs every 2 days and exports data from the app, save the file in a specific format with a specific header to that storage.
I know that I can write a class method which generates the format, use strategy pattern to get a specific header and provide it using respond_to and give a format csv so a user can access that file through a URL but how can I write a rake task which creates the file and uploads it to the specific location I specify ?
Will really appreciate any direction
In Rails rake tasks are usually stored in lib/tasks and have .rake extension. For the CSV part, you can use the CSV API of Ruby. After generating a CSV you can save it locally or upload it to any service you want (e.g. S3, Google Drive, etc). For example, take S3:
# lib/tasks/csv_tasks.rake
namespace :csv do
desc 'Generates new feed'
task :feed do
client = Aws::S3::Client.new(region: 'eu-west-1',
access_key_id: 'access_key_id',
secret_access_key: 'secret_access_key')
feed_csv = CSV.generate do |csv|
# headers
csv << [:first_name, :last_name]
# CSV rows
Employee.find_each do |employee|
csv << [employee.first_name, employee.last_name]
end
end
client.put_object({
body: feed_csv,
bucket: 'my_bucket',
key: 'feed.csv',
})
end
end
Then in Heroku scheduler use the defined task rake csv:feed
You might also consider having a model for your files in order to save their paths and then display them easily in your application.
I advise you to save your S3 or other credentials in the secrets.yml file or the credentials file (Rails 5.2+). To use the AWS SDK for Ruby, add this to your Gemfile:
gem 'aws-sdk', '~> 3'
And require it in the rake task, if needed. For more info about how to work with S3 and ruby, read here.

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.
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/

Uploaded CSV file parsing data

In my rails app, how can I access data from an already uploaded csv file? I have used paperclip to upload the file successfully, but struggling to find any tutorials or references on how to parse the data. I have seen http://railscasts.com/episodes/396-importing-csv-and-excel but this seems to be helpful for uploading into database columns.
From the API for the CSV class
Something along the lines of:
CSV.foreach("path/to/file.csv") do |row|
# use row here...
end

Resources