# Automated operations on a database

**URL:** <https://www.ruby-forum.com/t/automated-operations-on-a-database/104932>\
**Category:** Rails\
**Created:** [May 23, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/automated-operations-on-a-database/104932 "2007-05-23T23:55:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [May 23, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/automated-operations-on-a-database/104932/1 "2007-05-23T23:55:27Z")

</div>

Hello, i have a question about a problem i can’t figure out how to  
solve.

I have a standard RoR application made with the classic MVC model, and I  
need to (for example) edit a value in a table with a time-based rule.

EX:

starting table:

## user | money

```
    |

```

john | 500

after 1 minute:

## user | money

```
    |

```

john | 510

etc…

How to do that?  
Really thank you.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [May 24, 2007, 12:39am UTC](https://www.ruby-forum.com/t/automated-operations-on-a-database/104932/2 "2007-05-24T00:39:32Z")

</div>

I don’t know if there’s a more Rails or Ruby way to do this, but the  
best I’ve done is write a ruby script that bootstraps Rails, get my  
work done through ActiveRecord (so I don’t by pass any of my business  
logic), and then tell cron to run this script every so often.

The following code works for me in bootstrapping Rails:

#!/usr/bin/env ruby

# Ensure the environment was specified

if ARGV.length != 1  
puts “usage: ruby dummy\_records.rb \<rails\_env\>”  
exit 1  
end

$LOAD\_PATH \<\< File.expand\_path(File.dirname( **FILE** ))

ENV[‘RAILS\_ENV’] = ARGV.first || ENV[‘RAILS\_ENV’]

require ‘rubygems’  
require File.dirname( **FILE** ) + ‘/…/config/boot’  
require “#{RAILS\_ROOT}/config/environment”

def connect(environment)  
conf = YAML::load(File.open(File.dirname( **FILE** ) + ‘/…/config/  
database.yml’))  
ActiveRecord::Base.establish\_connection(conf[environment])  
end

# Open ActiveRecord connection

connect(ARGV.first)

# Enter code here to interact with your models.

Warm regards,

David R.

On May 23, 10:55 pm, Sonny C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
