I am building a demo, and I want to make it very easy for a non-technical person to set up and run the demo. I have built a seeds.rb file with lots of demo data in it. I want to be able to reset the rails app to a known state by providing an administrator-level action via a page link. I don't want to provide these non-tech demonstrators with a command line and rake, because they might shoot themselves in the foot.
I have looked into using load 'db/seeds.rb' within a method, but that doesn't quite do what I want. I know I am missing something, but what?
Using a custom Rails task to seed actual data To seed actual data, it is best to create a custom Rails task. Let's generate one to add genres. First generate the model and then migrate the database. Finally create the task.
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.
You can call Rails.application.load_seed
. That's all rake db:seed
does.
I prefer the classic method:
bundle exec rails db:seed
But I guess, that you can also call Rails.application.load_seed
as mentioned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With