# Using ActiveRecord in a script?

**URL:** <https://www.ruby-forum.com/t/using-activerecord-in-a-script/136438>\
**Category:** Rails\
**Created:** [May 2, 2008, 3:44am UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-script/136438 "2008-05-02T03:44:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 2, 2008, 3:44am UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-script/136438/1 "2008-05-02T03:44:26Z")

</div>

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 M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 2, 2008, 4:01am UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-script/136438/2 "2008-05-02T04:01:10Z")

</div>

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 M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 2, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-script/136438/3 "2008-05-02T21:16:20Z")

</div>

Jeff M. 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.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 2, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-script/136438/4 "2008-05-02T22:35:53Z")

</div>

That worked great! Thanks very much!

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 3, 2008, 12:27am UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-script/136438/5 "2008-05-03T00:27:30Z")

</div>

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 M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
