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.

10 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

  7. Admiring the time and effort you put into your blog and
    detailed information you present. It’s awesome to come across a blog every once in a while that isn’t the same outdated rehashed material.
    Fantastic read! I’ve saved your site and I’m including your RSS feeds to
    my Google account.

  8. I have seeded multiple times and the same data repeated on my web app. What can I do ?!

    • You need to take care for that in your seed data logic.

      For example:
      say you want to create a see user in DB with username jishnu.

      You should do like:
      user = User.find_or_initialize_by(username: ‘jishnu’)
      It will find the user and if not exist will create new.

      I have just given a example.

      Bottom line is that, first you should check if object exist or not.
      If not then only create a record.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s