Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I somehow execute my db/seeds.rb file from my rails app?

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?

like image 900
explainer Avatar asked Mar 06 '11 18:03

explainer


People also ask

How do I run a seed file in rails?

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.

What is seed RB file?

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.


2 Answers

You can call Rails.application.load_seed. That's all rake db:seed does.

like image 153
idlefingers Avatar answered Oct 03 '22 21:10

idlefingers


I prefer the classic method:

bundle exec rails db:seed 

But I guess, that you can also call Rails.application.load_seed as mentioned.

like image 33
facundofarias Avatar answered Oct 03 '22 19:10

facundofarias