Changing adapters in ActiveRecord does not change the generated sql

If I establish a new connection with a new adapter (ie. switch from
mysql to
postgresl) the generated sql is still mysql specific. I tried all sorts
reset methods on AR::Base but to no avail. There must be something
memoized on ActiveRecord base that has to do with the adapter and it
get cleared when you establish a connection with a new adapter.

I know this isn’t standard behaviour, in fact the only reason I need
this is
because I’m writing a multi-tenancy
gem rails that supports
many different database adapters. My tests run
through these adapters, but the 2nd adapter to be tested always fails
invalid sql.

Here’s an example:

ActiveRecord::Base.establish_connection :adapter => ‘mysql2’, :database
‘my_test_db’, :username => ‘root’, :password => ‘’
User.scoped.to_sql #=> "SELECT users.* FROM users"
ActiveRecord::Base.establish_connection :adapter => ‘postgresql’,
=> ‘my_test_db’, :username => ‘root’, :password => ‘’
User.scoped.to_sql #=> "SELECT users.* FROM users"

These back ticks on column names are mysql specific and are invalid for
postgresql, so it throws an error.

Does anyone know how to fully clear out whatever it is that is caching
sql being generated??

I searched through the source and was able to unset @quoted_table_name
the user model, this regenerated the table name properly, however the
now looks like:

User.scoped.to_sql #=> "SELECT “users”.* FROM users"

Still not perfect. And I also don’t want to have to go through each
and unset a bunch of vars. Any simpler way to reload these?

On Sep 13, 4:29pm, Bradley [email protected] wrote:

Still not perfect. And I also don’t want to have to go through each model
and unset a bunch of vars. Any simpler way to reload these?

You could try calling reset_column_information (although it looks like
this doesn’t clear out quoted table name)


ya that doesn’t appear to help. especially if I’m just selecting *

I started looking into the
as I figured maybe manually reloading the classes in between testing
adapters would help.

In the dev console doing a reload appears to be similar to resetting the
quoted_table_name, I get the same:

User.scoped.to_sql #=> "SELECT “users”.* FROM users"

So it’s caching that table_name (in the FROM clause) somewhere else
isn’t reloaded)

Does anyone have any insight into this? I’m digging into the source but
not having a ton of luck.

I’ve found out something very interesting. If I’m first connected to
Postgresql, then switch to Mysql, I can reset both table and column
properly using




This gives me the proper backquoted columns/table names for Mysql.

User.scoped.to_sql #=> "SELECT users.* FROM users"

If however, I switch back to Postgresql, and run the same commands, it
doesn’t actually reset the column information properly, only the

User.scoped.to_sql #=> "SELECT “users”.* FROM users"

I’ve found that it actually doesn’t matter in which order this happens.
first switch to one adapter allows me to reset properly. The second
however does NOT.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs