Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass an argument to rake db:seed?

Part of my seeds.rb loads a lot of data into the database. I want to be able to selectively load this data. E.g.

$ rake db:seed

or

$rake db:seed[0]

would just load the necessary data to run the site, while

$ rake db:seed[1]

would load my big data file into the database as well. Is this possible? How can I make this happen? If not, can anyone think of a way to do what I'm trying to do?

like image 467
Chris Avatar asked Jan 22 '13 02:01

Chris


People also ask

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.

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.


1 Answers

Rake arguments are painful to pass around, unfortunately (and db:seed doesn't pass its arguments through, regardless).

Your best bet is to use environment variables to pass your extra args through:

rake db:seed minimal=yes 

and

unless ENV["minimal"]   # do stuff 

etc

like image 57
Nevir Avatar answered Sep 19 '22 22:09

Nevir