January 25, 2012

//dennisreimann

Seeds for different environments

Here’s a little Rails tip for splitting up seed data for various environments: Create the folder db/seeds/. For each environment you want to seed put a file in there named after the env. Your general seeds are put into db/seeds/all.rb. Here’s what it might look like:

|___seeds
            | |___all.rb
            | |___development.rb
            | |___staging.rb
            | |___production.rb
            |___seeds.rb
            

Change the content of your db/seeds.rb to something like this:

['all', Rails.env].each do |seed|
              seed_file = "#{Rails.root}/db/seeds/#{seed}.rb"
              if File.exists?(seed_file)
                puts "*** Loading #{seed} seed data"
                require seed_file
              end
            end
            

Running rake db:seed will now load up the seed data defined in db/seeds/all.rb and the current environment.

January 25, 2012 12:59 PM