ActiveRecord without Rails: necessary to close connection?

M

Mischa Berger

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

Brian Candler

Mischa said:
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.
 
M

Mischa Berger

Brian said:
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??
 
M

Mischa Berger

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top