breaking into the unknown…

rake db:seed in rails


rake command is explained in this post . writing custom rake task is explained in this post.  db:seed is inbuilt rake task defined by rails. The rake db:seed command, basically execute whatever code you write in db/seeds.rb file of your application. Though can write any code in this file, by convention you should write code which populate your database with the basic data, for example: when ever your deploy your application somewhere, and create a new database for it, you want that user with admin credential must be present there. So you will write the code which create that user in this file. Below is the sample code which will create a user and assign admin role to him.

puts "********Seeding Data Start************"

admin = User.create(:first_name => 'System', :last_name => 'Admin', 
       :email => '', :password => 'sunpoweradmin', 
       :password_confirmation => 'sunpoweradmin', :source_system_id => 'systemadmin', 
       :source_system => 'LP',:entity_type => "Customer", :target_system => "OPENAM")

if admin.errors.blank?
    puts "***User #{admin.first_name} #{admin.last_name} created ***"
    admin.add_role :admin # add_role is method defined by rolify gem
    puts "***admin role assigned to #{admin.first_name} #{admin.last_name}***"
    puts "admin user failed to create due to below reasons:"
    admin.errors.each do |x, y|
       puts"#{x} #{y}" # x will be the field name and y will be the error on it

puts "********Seeding Data End************"

Now whenever you recreate your database, you just need to run below command to populate the database, with the basic data

$ rake db:seed RAILS_ENV=production

The correct order to setup database in production, with all the rake task available within db namespace is as below

$rake db:create RAILS_ENV=production

$rake db:migrate RAILS_ENV=production

$ rake db:seed RAILS_ENV=production

NOTE: You can replace the first two commands with $rake db:setup RAILS_ENV=production , it will run both create and migrate internally

=> COMPLETE LIST of rake db command in rails :

db:create creates the database for the current env
db:create:all creates the databases for all envs
db:drop drops the database for the current env
db:drop:all drops the databases for all envs
db:migrate runs migrations for the current env that have not run yet
db:migrate:up runs one specific migration
db:migrate:down rolls back one specific migration
db:migrate:status shows current migration status
db:migrate:rollback rolls back the last migration
db:forward advances the current schema version to the next one
db:seed (only) runs the db/seed.rb file
db:schema:load loads the schema into the current env’s database

db:schema:dump dumps the current env’s schema (and seems to create the db as well)

db:setup runs db:schema:load, db:seed
db:reset runs db:drop db:setup
db:migrate:redo runs (db:migrate:down db:migrate:up) or (db:migrate:rollback db:migrate:migrate) depending on the specified migration
db:migrate:reset runs db:drop db:create db:migrate

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

7 thoughts on “rake db:seed in rails

  1. Nice post . It is really help full . need one more about rake db:seeds if we use Mongodb in rails application.. Thanks

  2. hey there and thank you for your info – I’ve certainly picked up something new from right here. I did however expertise several technical issues using this site, as I experienced to reload the website many times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement
    in google and can damage your high quality score if ads and marketing with
    Adwords. Well I am adding this RSS to my email and can look out
    for much more of your respective interesting content.
    Make sure you update this again soon.

  3. Reblogged this on universityhelper and commented:
    This is from the blog . Its about the rake in rails . rake db:seed

  4. Hi thank you for your review! I have a problem when I do rake db:seed => it shows NameError: uninitialized constant Exercice
    Do you know what is the error could come from? Thank you

  5. Tahnk you it works arunyadadav4u

  6. Thank ya

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s