RoR Database seeding
Lately, I’m researching various RoR methods, technologies, and doing further ready on Agile software development methodologies. Below is a technique I’ve learned for seeding RoR databases.
- Database seeding technique
- Define the model and controller (nifty_scaffold is great for building quick, CRUD interfaces)
- Add data to the database via the CRUD interface
- Use sqlite3 command to extract the new seed data into a flat file.
In this example, the table contains custom fields for name and description.cd [project folder]\db sqlite3 development.sqlite3 .output [TABLE].txt; SELECT ID,name,description FROM [TABLE]; .quit
- Add import code to seeds.rb
JobPayCycle.delete_all open('db/job_pay_cycles.txt') do |records| records.read.each_line do |line| id,value,description = line Visit Website.chomp.split("|") JobPayCycle.create!(:id=>id,:value=>value,:description=>description) end end
- Push the code into revision control (seeds and the new file
- Inform other developers.
- Other developers perform the following steps
- Other developers retrieve from revision control
- rake db:migrate — create the database tables for new models
- rake db:seed — to seed the new tables