DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

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

  • submit to reddit
        Found this on the #rubyonrails irc log here - 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.


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