Forum: Ruby ActiveRecord without Rails: necessary to close connection?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
181035f8aac1cb68d50dc58b67dc0349?d=identicon&s=25 Mischa Berger (mischa78)
on 2009-04-15 07:45
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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-04-15 11:24
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.
181035f8aac1cb68d50dc58b67dc0349?d=identicon&s=25 Mischa Berger (mischa78)
on 2009-04-15 12:07
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??
181035f8aac1cb68d50dc58b67dc0349?d=identicon&s=25 Mischa Berger (mischa78)
on 2009-04-15 14:22
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 :-)
This topic is locked and can not be replied to.