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

  1. Define the model and controller (nifty_scaffold is great for building quick, CRUD interfaces)
  2. Add data to the database via the CRUD interface
  3. 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
    
  4. 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
    
  5. Push the code into revision control (seeds and the new file
  6. Inform other developers.
      Other developers perform the following steps

    1. Other developers retrieve from revision control
    2. rake db:migrate — create the database tables for new models
    3. rake db:seed — to seed the new tables

Leave a Reply

Your email address will not be published. Required fields are marked *