Ruby Forum Ruby on Rails > Using ActiveRecord in a script?

Posted by Jeff Miller (ibanez270dx)
on 02.05.2008 03:44
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
Posted by Roger Pack (rogerdpack)
on 02.05.2008 04:01
(Received via mailing list)
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

On Thu, May 1, 2008 at 7:44 PM, Jeff Miller
Posted by Matthew Rudy Jacobs (matthewrudy)
on 02.05.2008 21:16
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.

Posted by Jeff Miller (ibanez270dx)
on 02.05.2008 22:35
That worked great! Thanks very much!
Posted by Paul Lynch (plynchnlm)
on 03.05.2008 00:27
(Received via mailing list)
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.

On May 2, 4:35 pm, Jeff Miller <rails-mailing-l...@andreas-s.net>