Reputation: 55
I am looking for the best way to populate the reference tables in my rails application - for dev, test, and production (the most important, of course, being for production). (Ideally, I think using essentially the same procedure for all three would be the best, or at least, easiest solution; but I suspect this may not be practical or wise.)
For example, I am about to create a table for basic stock information that usually doesn't change (fields: symbol, name, sector, industry). I need this table to be loaded (for example, via a text file containing this data for all US stocks) the first time the application is run in production, as well as in testing and development whenever the table has not yet been loaded. (In case it matters re. my question, it's extremely likely this table will be updated as new data [e.g., for a company that just went public] becomes available.)
I was surprised that I didn't find this question already in stackoverflow. It might be in there, but it's not findable with the searches I tried. I found a couple similar questions (e.g., seeding test data), but not close enough to provide what I'm looking for. I couldn't even find much via google searches! I did, however, find this: https://codingitwrong.com/2016/07/29/how-to-seed-and-migrate-rails-data.html In particular, the author says: "The seeds file is commonly used for both of these [reference data vs sample data], but that makes it difficult to set up initial data in production. The Rails guides don’t make it clear which of these the seeds file is for... It can be useful to separate the two out. Thoughtbot recommends using the seeds file for reference data, and setting up a separate Rake task for sample data."
(Note: Based on the above quote I googled for "rails what is the seeds.rb file for?" and found this Q/A: What is the function of the seeds.rb file?, which recommends using seeds.rb for reference data. So perhaps using seeds.rb is the best answer. If so, that appears to imply that seeds.rb should be used for reference data and not test setup.)
Upvotes: 1
Views: 775
Reputation: 1549
Create a CSV with all required data. Then keep the CSV inside db
folder.
# db/seeds.rb
require 'csv'
csv_text = File.read(Rails.root.join('db','file_name.csv'))
Then Loop through the CSV data with the below code:
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
puts row.to_hash
# each row will be a hash object. Use the data in this hash object to create records in the DB.
end
Make sure that the CSV file has headers in its first row.
Now you can use rails db:seed
to import the CSV file data into your DB.
Upvotes: 1
Reputation: 1002
What I understand from your question is, you want a way to initialize the table in bulk for the first time.
You can create an excel file of the records of that table, and then import that excel file into your app, and creating records for each entry in the excel file.
This railscasts video covers the basic usage of a gem named roo which provides this functionality.
Upvotes: 0