# Batch processes interacting with ROR data

**URL:** <https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058>\
**Category:** Rails\
**Created:** [November 23, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058 "2006-11-23T20:11:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [November 23, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058/1 "2006-11-23T20:11:48Z")

</div>

The application I am working on needs a nightly process to comb through  
the database and make periodic changes.

What is the best way to implement a script in order to leverage the  
existing database connection management and active record  
functionality?

Thanks.

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [November 23, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058/2 "2006-11-23T22:08:00Z")

</div>

Dan M. wrote:

> The application I am working on needs a nightly process to comb through  
> the database and make periodic changes.
> 
> What is the best way to implement a script in order to leverage the  
> existing database connection management and active record  
> functionality?

Tell the OS to periodically run a script using ‘cron’, or the Task  
Manager,  
or some derivative therof. Some are called ‘at’.

The script may run something like this:

ruby script/runner “load(‘my\_batch.rb’)”

my\_batch.rb then contains a batch of Ruby, enjoying the complete Rails  
context. IIRC.

There might be other ways to invoke the runner script. --help didn’t  
confess  
them.

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [November 24, 2006, 3:24am UTC](https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058/3 "2006-11-24T03:24:31Z")

</div>

Thanks Phlip,

I have no problem with the scheduling of the script. I really want to  
understand how people are writing the scripts themselves share the  
existing database connections and use active record. For instance:

1. Periodic web service invocation.
2. External script.

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [November 24, 2006, 5:18am UTC](https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058/4 "2006-11-24T05:18:47Z")

</div>

Fred wrote:

> I think you’re looking for difficulties where there are none.  
> The script run by cron (for example) will have its own database  
> connection, just as each of your mongrels/fcgi listeners has one, and  
> can use ActiveRecord just as your controllers would or you would from  
> script/console.

I solved that one, and re-used the connection. I did it the extra-sick  
way.  
I wrote a private action, available only in non-production mode, and  
then I  
hit it with a simulated web browser.

There’s a less-sick way, right?

But all the pieces were just laying around, tempting me, saying “put me  
together like thiiiiis!!”

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [November 24, 2006, 10:19am UTC](https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058/5 "2006-11-24T10:19:13Z")

</div>

There are examples of how to use ActiveRecord outside of rails in the  
Rails Recipes book, it may be of use.

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [November 24, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058/6 "2006-11-24T12:49:31Z")

</div>

Dan M. wrote:

> Thanks Phlip,
> 
> I have no problem with the scheduling of the script. I really want to  
> understand how people are writing the scripts themselves share the  
> existing database connections and use active record. For instance:
> 
> 1. Periodic web service invocation.
> 2. External script.

This is Rails - you don’t share anything other than the database  
itself. Batch scripts and offload scripts run in their own separate  
process space unhindered by the Web facing application processes.

NeilW

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [November 24, 2006, 4:40am UTC](https://www.ruby-forum.com/t/batch-processes-interacting-with-ror-data/77058/7 "2006-11-24T04:40:22Z")

</div>

I think you’re looking for difficulties where there are none.  
The script run by cron (for example) will have its own database  
connection, just as each of your mongrels/fcgi listeners has one, and  
can use ActiveRecord just as your controllers would or you would from  
script/console.

Fred
