Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of seeds in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 427
    Comment on it

    Hi Friends,

    Few days back I was facing an issue that once I was creating an application in development mode, I needed to sign up to my application with fake emails, so that was very troublesome process, so I found out about seeds in rails.


    There is a file in seeds.rb inside db folder that automatically gets built once you create a rails application which is actually used for creating seed data to database which is actually used for multiple purposes:


    1. You can write active record queries inside the seeds.rb

    2. This can be used populate some fake data for testing purposes

    3. This can also be used to populate some dependent/default data for the application like languages data, country data etc.


    The process of creating seed data is very simple. Suppose you want to create 3 user entries using seed. You can write that query in db/seeds.rb like this:

    User.create(email: "user1@domain.com")
    User.create(email: "user2@domain.com")
    User.create(email: "user3@domain.com")


    Now once you set up your application first set up your database using

    rake db:setup
    ## It will create the database if doesn't exist and will run all the pending migrations
    

    Now run the seed using
     

    rake db:seed
    ## It will run the code written in seeds.rb and hence will create the entries for the users

     

    Taking back up of table in seeds.rb format:

    Once reading through a blog I also got to know that we can also take a back up of a data in seeds.rb format so that we can run it and populate the same data anywhere else. Lets take an example of that, suppose we have an existing application that has so many users in which few users are admin, we don't want to take backup of all the users, we just want to take out the admin's from that and create seed data for them so that they can be added using seed in other application. For that here I have created a task like inside lib/tasks/export_data.rake

    namespace :export_data do
      desc "Prints User.all in a seeds.rb"
      task :inside_seeds => :environment do
        User.where(is_admin: true).each do |user|
          puts "User.create(#{user.serializable_hash.delete_if {|key, value| ['created_at','updated_at','id'].include?(key)}.to_s.gsub(/[{}]/,'')})"
        end
      end
    end


    Now once we run this task we can take the output in a file that can be used as seeds.rb

    rake export_data:inside_seeds > db/seeds.rb
    
    ## This will give the output like this in db/seeds.rb
    
    User.create("email"=>"user1@domain.com")
    User.create("email"=>"user2@domain.com")
    User.create("email"=>"user3@domain.com")
    

    We can use this content to create users from seeds data.

    Hope you liked this blog.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: