# ActiveRecord outside of rails

**URL:** <https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307>\
**Category:** Rails\
**Created:** [December 14, 2006, 3:45am UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307 "2006-12-14T03:45:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 14, 2006, 3:45am UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/1 "2006-12-14T03:45:06Z")

</div>

I have a few questions regarding using activerecord outside of rails.

Usage is with legacy databases, which gives rise to some of the  
problems.

Probably the first, and most simple question though, is how to capture  
the SQL log, with which I can hopefully figure the other stuff out.

I’m currently trying

ActiveRecord::Base.logger = Logger.new STDOUT

Based on random googles, but it doesn’t seem to give me anything.

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 16, 2006, 5:00am UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/2 "2006-12-16T05:00:45Z")

</div>

Charles L. wrote:

> I have a few questions regarding using activerecord outside of rails.
> 
> Usage is with legacy databases, which gives rise to some of the  
> problems.
> 
> Probably the first, and most simple question though, is how to capture  
> the SQL log, with which I can hopefully figure the other stuff out.
> 
> I’m currently trying
> 
> ActiveRecord::Base.logger = Logger.new STDOUT
> 
> Based on random googles, but it doesn’t seem to give me anything.

Is there a different list I could perhaps put this question too?

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 16, 2006, 5:17am UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/3 "2006-12-16T05:17:13Z")

</div>

On Dec 15, 2006, at 11:00 PM, Guest wrote:

> > I’m currently trying
> > 
> > ActiveRecord::Base.logger = Logger.new STDOUT
> > 
> > Based on random googles, but it doesn’t seem to give me anything.
> 
> Is there a different list I could perhaps put this question too?

I used this successfully for a script recently:

ActiveRecord::Base.logger = Logger.new( “my\_app.log” )

Does that help?

Craig

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 18, 2006, 5:29am UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/4 "2006-12-18T05:29:23Z")

</div>

> ActiveRecord::Base.logger is passed into your connection adapter at the  
> time of instantiation. If you change your class “logger” object, it is  
> not reflected in the adapter. This is a bug in rails IMO.
> 
> For a temporary workaround you can use:  
> ModelClass.connection.instance\_eval( “@logger=Logger.new( STDOUT )” )
> 
> I will submit ticket and patch tomorrow.
> 
> Zach

Does this mean if I write  
ActiveRecord::Base.logger = …

before I define my class:  
class Document \< ActiveRecord::Base  
…

That it should work also?  
At any rate, trying that, and what you suggested doesn’t give me  
anything.  
Maybe I’m mis-understanding it:

$log = Logger.new STDOUT  
$log.level # =\> 0  
$log.warn ‘hello world’ # =\> hello world  
ActiveRecord::Base.logger = $log  
Document.connection.instance\_eval(’@logger = $log’)

# test some more

Document.connection.instance\_eval(’@logger’).warn ‘hello world’ # =\>  
hello world

# now this should output some sql no?

Document.find :first

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 16, 2006, 6:47am UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/5 "2006-12-16T06:47:17Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Guest wrote:

> > ActiveRecord::Base.logger = Logger.new STDOUT
> > 
> > Based on random googles, but it doesn’t seem to give me anything.
> 
> Is there a different list I could perhaps put this question too?

ActiveRecord::Base.logger is passed into your connection adapter at the  
time of instantiation. If you change your class “logger” object, it is  
not reflected in the adapter. This is a bug in rails IMO.

For a temporary workaround you can use:  
ModelClass.connection.instance\_eval( “@logger=Logger.new( STDOUT )” )

I will submit ticket and patch tomorrow.

Zach

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.3 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFg4mKMyx0fW1d8G0RAoKyAJ48As1jEWD8ftitmmLeXRjNxfjTTwCcCDyG  
uLkgNwkzXzs8VpCBI8tU9Cw=  
=RYig  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 18, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/6 "2006-12-18T23:41:20Z")

</div>

First of all, thanks for your help, I’ve been trying to figure this out  
for about a week with little success. Seems like it (AR) could be a  
really nice abstraction, I just haven’t got it working quite right  
yet…

zdennis wrote:

> > > ActiveRecord::VERSION::STRING  
> > > =\> “1.14.4”
> 
> What version of ActiveRecord are you using?
> 
> Zach

For me,  
ActiveRecord::VERSION::STRING  
=\> “1.14.4”

FWIW, i’m using ruby 1.8.4, on cygwin.  
I installed AR using rubygems, then installed OpenLink’s ODBC connection  
adapter (is it the adapter’s job to send the SQL to the logger?) and  
finally monkey-patched it a bit, in a similar fashion to dbi’s ODBC  
class, to allow it to use odbc\_utf8, and to allow the use of simple dsns  
in string form.

I connect using:  
ActiveRecord::Base.establish\_connection :adapter =\> ‘odbc’,  
:dsn =\> ‘driver=Microsoft Access Driver (\*.mdb); dbq=’ +  
mdb\_filename  
(All this is in aid of writing a script that works on someone else’s  
Access database.)

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 18, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/7 "2006-12-18T13:19:00Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Charles L. wrote:

> > 
> 
> Does this mean if I write  
> ActiveRecord::Base.logger = …
> 
> before I define my class:  
> class Document \< ActiveRecord::Base  
> …
> 
> That it should work also?

Yes. It should also work if you define it on  
ActiveRecord::Base.connection after you define your models as well.

> Document.connection.instance\_eval(‘@logger’).warn ‘hello world’ # =\>  
> hello world
> 
> # now this should output some sql no?
> 
> Document.find :first

Yes it should.

> > ActiveRecord::Base.logger = Logger.new( STDOUT )  
> > =\> #\<Logger:0xb7754e70 @formatter=nil,  
> > @default\_formatter=#\<Logger::Formatter:0xb7754e48 @datetime\_format=nil\>,  
> > level0,  
> > prognamenil, logdev#\<Logger::LogDevice:0xb7754e20 @shift\_age=nil,  
> > @filename=nil,  
> > @mutex=#\<Logger::LogDevice::LogDeviceMutex:0xb7754df8  
> > @mon\_waiting\_queue=[], @mon\_entering\_queue=[], @mon\_count=0,  
> > @mon\_owner=nil\>, dev#\<IO:0xb7cce02c, @shift\_size=nil\>  
> > class Developer \< ActiveRecord::Base ; end  
> > =\> nil  
> > Developer.count  
> > SQL (0.058811) SET NAMES ‘utf8’  
> > Developer Columns (0.183238) SHOW FIELDS FROM developers  
> > SQL (0.001609) SELECT count(\*) AS count\_all FROM developers  
> > =\> 3  
> > Developer.find( :all )  
> > Developer Load (0.024063) SELECT \* FROM developers  
> > =\> [#\<Developer:0xb7744390 @attributes={“name”=\>“Zach D.”,  
> > “updated\_at”=\>nil, “salary”=\>“1”, “id”=\>“1”, “created\_at”=\>nil},  
> > #\<Developer:0xb7744354 @attributes={“name”=\>“John D.”,  
> > “updated\_at”=\>nil, “salary”=\>“2”, “id”=\>“2”, “created\_at”=\>nil},  
> > #\<Developer:0xb7744318 @attributes={“name”=\>nil, “updated\_at”=\>nil,  
> > “salary”=\>“3”, “id”=\>“3”, “created\_at”=\>nil}]  
> > ActiveRecord::VERSION::STRING  
> > =\> “1.14.4”

What version of ActiveRecord are you using?

Zach

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.3 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFhoc2Myx0fW1d8G0RAmanAJ4j+CczQ5zhBo5SwQ+1B1ta4jOZLQCdHx3O  
3NVayDtqlqqlW9Qpa9pO3ow=  
=YOG4  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 19, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/8 "2006-12-19T23:14:30Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Charles L. wrote:

> For me,  
> ActiveRecord::VERSION::STRING  
> =\> “1.14.4”
> 
> FWIW, i’m using ruby 1.8.4, on cygwin.  
> I installed AR using rubygems, then installed OpenLink’s ODBC connection  
> adapter (is it the adapter’s job to send the SQL to the logger?)

Yes it is the adapters responsbility to log the SQL. This usually  
occurs in an ‘execute’ method on the implemented adapter.

Can you send me your adapter code ?

Zach  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.3 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFhyK/Myx0fW1d8G0RAnqvAJ0QGW4u66eBk/6YJfFF99tkNhzraQCfULZc  
D5Id7mZrTRyLBGKg7YdKe98=  
=PecN  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [May 23, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/9 "2007-05-23T20:49:58Z")

</div>

Hi,

I saw your Dec. 2006 post that mentioned connecting via ‘ODBC’ to a  
Microsoft Access db. I know it was a while ago but I am new to Rails  
and would like to connect to a legacy systems Access db and could use  
some help. I have installed the adapters but I am unsure of what  
other things need to be done. Does the “establish\_connection” go in a  
model named for the Access db? What if anything goes in the database  
yml?  
Thanks.

Lewis

On Dec 18 2006, 6:41 pm, Charles L. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![calande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@calande](https://www.ruby-forum.com/u/calande)\
**Post date:** [December 20, 2006, 5:12am UTC](https://www.ruby-forum.com/t/activerecord-outside-of-rails/79307/10 "2006-12-20T05:12:17Z")

</div>

zdennis wrote:

> Yes it is the adapters responsbility to log the SQL. This usually  
> occurs in an ‘execute’ method on the implemented adapter.
> 
> Can you send me your adapter code ?
> 
> Zach

Ahh, so that’d be the problem then. I see the SQLite adapter is doing  
some definite logging:

# sqlite:

def execute(sql, name = nil) #:nodoc:  
catch\_schema\_changes { log(sql, name) { @connection.execute(sql) } }  
end

# odbc:

def execute(sql, name = nil)  
@logger.unknown(“ODBCAdapter#execute\>”) if @@trace  
@logger.unknown(“args=[#{sql}|#{name}]”) if @@trace  
if sql =~ /^\s\*INSERT/i && [:microsoftsqlserver,  
:virtuoso].include?(@dbmsName)  
# Guard against IDENTITY insert problems caused by explicit inserts  
# into autoincrementing id column.  
insert(sql, name)  
else  
begin  
@connection.do(sql)  
rescue Exception =\> e  
@logger.unknown(“exception=#{e}”) if @@trace  
raise StatementInvalid, e.message  
end  
end  
end

Further more, while the sqlite functions such as #select\_all, go through  
execute, the odbc version doesn’t.  
I suppose I can set @@trace variable…  
Is there an official odbc connector?
