Topic: Ruby script (seperate to my Rails project) to use my Rails Models?

I have a script which needs to read in a CSV file, then manipulate some data in my database.  This only has to happen once of.  About once/twice a year.

So I have created the Ruby script, placing it in a directory (/scripts) inside my Rails project's root directory, but it can't find my Models.

I suspect I have to "require" something at the top of the script or something?  Any help would be appreciated.

Thank You

Re: Ruby script (seperate to my Rails project) to use my Rails Models?

create a file in /lib/tasks called "mytasks.rake"

in this file, put this:


namespace :my_tasks do
  desc "A task that does something to the DB once or twice a year"
  task :do_something => :environment do
    #your scripts code goes here.
  end
end

Ther namespace is to keep your methods grouped and prevent interferring with other tasks hat have the same name.

All your models, Mailers etc. will be available just like in the rails console. you can the run this task with a simple

rake my_tasks:do_something

from the rails root directory

Re: Ruby script (seperate to my Rails project) to use my Rails Models?

This solved the problem.

Thank You

Re: Ruby script (seperate to my Rails project) to use my Rails Models?

Also, if you put this file in the lib directory it should be able to find it. If you want to put it in some other custom directory, you have to update your environment.rb file to include that path