ActiveRecord without Rails: necessary to close connection?

Hello everyone,

This is the first time I want to use ActiveRecord outside Rails. This is
my (simplified) code:

ActiveRecord::Base.establish_connection(
:adapter => ‘mysql’,
:host => ‘localhost’,
:username => ‘username’,
:password => ‘password’,
:database => ‘database’)

class Rate < ActiveRecord::Base
end

r = Rate.find(:first)
r.value = new_rate
r.save!

This works fine, but I was wondering if I have to close any database
connections at the end of my script (by using remove_connection??)? Or
is does AR take care of this automatically?

Thanks in advance.

Mischa.

Mischa B. wrote:

This works fine, but I was wondering if I have to close any database
connections at the end of my script (by using remove_connection??)? Or
is does AR take care of this automatically?

Don’t bother. The operating system will close all open filehandles when
the process terminates.

Brian C. wrote:

Don’t bother. The operating system will close all open filehandles when
the process terminates.

Thanks. With the same script I have another issue. When I execute the
script from the command line it runs flawlessly, but when it’s executed
from cron I get the following error in my script: no such file to load
– mysql

The line where the error occurs is:
r = Rate.find(:first)

My crontab looks like this:

          • /usr/local/bin/ruby
            /home/mischa/railsapps/aexscript/periodic/get_rate.rb production >>
            /home/mischa/railsapps/aexscript/periodic/get_rate.log

So I use full path to everything.

Any ideas??

I got it working. It turned out the mysql gem wasn’t in the expected gem
folder. Why everything works smoothly from the command line is beyond
me, but it works :slight_smile: