ActiveRecord without Rails: necessary to close connection?

Discussion in 'Ruby' started by Mischa Berger, Apr 15, 2009.

  1. 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-forum.com/.
     
    Mischa Berger, Apr 15, 2009
    #1
    1. Advertising

  2. 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-forum.com/.
     
    Brian Candler, Apr 15, 2009
    #2
    1. Advertising

  3. Re: ActiveRecord without Rails: necessary to close connectio

    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-forum.com/.
     
    Mischa Berger, Apr 15, 2009
    #3
  4. Re: ActiveRecord without Rails: necessary to close connectio

    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-forum.com/.
     
    Mischa Berger, Apr 15, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Drew Olson

    ActiveRecord/Oracle without Rails

    Drew Olson, Feb 7, 2007, in forum: Ruby
    Replies:
    9
    Views:
    222
    Jesse Hu
    Aug 26, 2008
  2. Ari Brown

    ActiveRecord without Rails

    Ari Brown, Jul 31, 2007, in forum: Ruby
    Replies:
    4
    Views:
    166
    Reid Thompson
    Jul 31, 2007
  3. Xeno Campanoli
    Replies:
    1
    Views:
    158
    Xeno Campanoli
    May 29, 2008
  4. Xeno Campanoli
    Replies:
    1
    Views:
    143
    Xeno Campanoli
    Jun 3, 2008
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    940
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page