I want to populate a new feature with dummy data, but don't want to use the db/seeds.rb file as it already has seeds other data irrelevant for this feature.
To run the default seeds.rb
file, you run the command rake db:seed
.
If I create a file in the db
directory called seeds_feature_x.rb
, what would the rake command look like to run (only) that file?
Start by creating a separate directory to hold your custom seeds – this example uses
db/seeds
. Then, create a custom task by adding a rakefile to yourlib/tasks
directory:This rakefile accepts the name of a seed file in the
db/seeds
directory (excluding the.rb
extension), then runs it as it would runseeds.rb
. You can execute the rake task by issuing the following from the command line:Update: Now it should also list the seed tasks when running
rake --tasks
orrake -T
.Too complicated! I just wanted a simple task to execute every file under db/seeds directory without passing in any file names.
I tried out zeantsoi's answer but it didn't give me what I wanted, it did all files in a directory. Hacked away at it and got this.
And to use this do the following:
This will look in the Rails.root/db/seeds folder for a file name without the .seeds.rb (it adds those for you).
Working example:
The above would seed the
Rails.root/db/seeds/my_custom_seed.seeds.rb
file