[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ActiveRecord without Rails: necessary to close connection?

Mischa Berger

4/15/2009 5:46:00 AM

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.
--
Posted via http://www.ruby-....

3 Answers

Brian Candler

4/15/2009 9:25:00 AM

0

Mischa Berger 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.
--
Posted via http://www.ruby-....

Mischa Berger

4/15/2009 10:08:00 AM

0

Brian Candler 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??

--
Posted via http://www.ruby-....

Mischa Berger

4/15/2009 12:23:00 PM

0

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 :-)
--
Posted via http://www.ruby-....