47

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?

explainer
  • 1,345
  • 3
  • 11
  • 25

2 Answers2

95

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

idlefingers
  • 31,659
  • 5
  • 82
  • 68
  • I just brought up my app with mongrel, opened a console, and typed 'Rails.application.load_seed'. I got the following: NoMethodError: undefined method `load_seed' for # from /home/kenb/ruby/gems/gems/railties-3.0.3/lib/rails/application.rb:77:in `send' from /home/kenb/ruby/gems/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing' from (irb):18 from :0 So, that didn't work. What am I missing, besides 'load_seed'? – explainer Mar 08 '11 at 16:01
  • 3
    Sorry, it looks like `load_seed` won't be available until 3.1.0 (I found it from the master branch of Rails and didn't think to check it). In the meantime, you'll have to run it with: `load(Rails.root.join("db", "seeds.rb"))` – idlefingers Mar 08 '11 at 16:13
5

I prefer the classic method:

bundle exec rails db:seed

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

facundofarias
  • 2,973
  • 28
  • 27