Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use seed.rb to selectively populate development and/or production databases?

I am using seed.rb to populate both my development and production database. I usually populate the first with dummy data and the latter with the real minimal data that my app needs to run (e.g. the first user and so on).

How can I specify in seed.rb for what environment each data is?

Given that I know "group" to be a Gemfile method, I'd like to achieve the same behavior for seed.rb.

E.g. I'd like to write something like this in my seed.rb:

group :development do    # development specific seeding code end  group :production do    # production specific seeding code end  # non-specific seeding code (it always runs)  

This to be able to call both the development-specific and the non-specific code with

$ rake db:seed 

And to call both the production-specific and the non-specific code with:

$ rake db:seed RAILS_ENV=production  

Thank you

like image 411
Darme Avatar asked Dec 28 '11 22:12

Darme


People also ask

What is DB seeds rb?

The seeds.rb file is where the seed data is stored, but you need to run the appropriate rake task to actually use the seed data. Using rake -T in your project directory shows information about following tasks: rake db:seed. Load the seed data from db/seeds.rb.

What does Rails DB seed do?

Rails seed files are a useful way of populating a database with the initial data needed for a Rails project. The Rails db/seeds. rb file contains plain Ruby code and can be run with the Rails-default rails db:seed task.


1 Answers

seeds.rb is just a plain ruby file, so there are several ways you could approach this. How about a case statement?

# do common stuff here  case Rails.env when "development"    ... when "production"    ... end 
like image 143
cam Avatar answered Sep 21 '22 19:09

cam