randomguy
randomguy

Reputation: 12252

ActiveRecord::Base.establish_connection(:another_db) is not working

Two databases are defined in database.yml, the production (default) and slave.

Using console to execute ActiveRecord::Base.establish_connection(:slave) and then User.count, the query is still executed on the production.

Why does this happen and how the query be made to run on slave?

Upvotes: 1

Views: 811

Answers (1)

BookOfGreg
BookOfGreg

Reputation: 3716

Add a method on User to toggle the connection

class User < ActiveRecord::Base
  def self.connect_to_slave
    establish_connection :slave
  end
  def self.connect_to_prod
    establish_connection :production
  end
end

Upvotes: 1

Related Questions