Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

How to get the underlying database connection out of ActiveRecord::Base (See related posts)

Found this on the #rubyonrails irc log here http://www.loglibrary.com/show_page/view/62?Multiplier=60&Interval=18&StartTime=1115561823 - credit goes to <tufty>.

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.


You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts