Ad

How To Use Rails'Active Record In A Separated Script.rb?

I have to run this script: /scripts/saveData.rb

And in it, I need to use ActiveRecord of my Rails application. I tried to invoke Class, but I get error "uninitialized constant (NameError)".

How can I get a reference to the Rails classes from my saveData.rb script?

Rails 3.1

UPDATED: how i can invoke a method of a Model? My model is: "Program" (also Active Record). I tried with "Program.method" but it doesnt works, why?

Ad

Answer

Which version of rails you are using?

You can run your code in Rails runner like this if you are using Rails 3:

http://guides.rubyonrails.org/command_line.html#rails-runner

$ rails runner script/saveData.rb

If you are using Rails 2, try this one:

$ script/runner script/saveData.rb
Ad
source: stackoverflow.com
Ad