Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How To Get The Underlying Database Connection Out Of ActiveRecord::Base

DZone's Guide to

How To Get The Underlying Database Connection Out Of ActiveRecord::Base

·
Free Resource
Found this on the #rubyonrails irc log here http://www.loglibrary.com/show_page/view/62?Multiplier=60∬erval=18⋆tTime=1115561823 - credit goes to 
 
  .

Problem: you want to get at the underlying database connection object, e.g. so you can do something like create_function or create_aggregate on a SQLite database.  You try ActiveRecord::Base.connection but that just gives you the Rails connection adapter, not the underlying database connection.

Solution:


  db = ActiveRecord::Base.connection.instance_variable_get(:@connection)
db.create_aggregate("xxx", 1) do
etc.


 
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}