14

I have 2 jobs I would like to run and they are dependant on Models in my rails application. I added the ruby files in a separate folder called Jobs, that I have appended to the rail project.

Whenever I try to run them via ruby command I get the following error:

uninitialized constant Feedback (NameError).

Feedback here is a model I'm using in my rails app.

My questions: because the jobs I'm using are actually compatible with the script/runner command of rails 2, is there an alternative with Rails 3? If not how can I write ruby programs that depend on models I have in a rails app without getting the error I mentioned above.

tshepang
  • 12,111
  • 21
  • 91
  • 136
mabounassif
  • 2,311
  • 6
  • 29
  • 46

2 Answers2

24

Use rails runner

$ rails -h
Usage: rails COMMAND [ARGS]
...
runner       Run a piece of code in the application environment

All commands can be run with -h for more information.
Jakob S
  • 19,575
  • 3
  • 40
  • 38
3

The "Rails 3 way" to do this is with Rake using the :environment prerequisite, which loads the Rails environment. Like so:

task :name => :environment do |t|
  # actions
end

In the block you can load and execute your jobs.

If you haven't written Rake scripts before, here's a good tutorial. It's quite easy.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182