Using ActiveRecord in a script?

Hello, ...at least I'm pretty sure its called ActiveRecord. I have an app running on InstantRails 1.6 that will periodically call a ruby script in the /script folder. In the script, I use the same models to connect to my database as the ones in the rest of the app, but it doesn't seem to work. I'm using "Model.connection.select_all('sql statement')" where Model is the name of the model. Do I have to call a special gem or something?

Thanks, - Jeff Miller

does it call the script as in system(script_name)? If so then that script would need to load up the whole AR framework somehow, at its start. -R

Jeff Miller wrote:

Hello, ...at least I'm pretty sure its called ActiveRecord. I have an app running on InstantRails 1.6 that will periodically call a ruby script in the /script folder. In the script, I use the same models to connect to my database as the ones in the rest of the app, but it doesn't seem to work. I'm using "Model.connection.select_all('sql statement')" where Model is the name of the model. Do I have to call a special gem or something?

you need to include your "environment.rb"

sticking this line; require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

at the top of your file should load up rails, giving you access to all of your models.

That worked great! Thanks very much!

I think another way to do this is with script/runner. I think you can use script runner in place of sh or tcsh (e.g. #! script/runner as the first line of the script file) and that is supposed to load the environment. See script/runner -help. I haven't tried it myself yet, but was just learning about it.