DB connection problem in production
I have a separate DB for one model in my application and in development the connection mode works correctly in production, however it does not.
production:
adapter: mysql
host: myhost
username: root
password:
database: production_db
users_production:
adapter: mysql
host: myhost
username: root
password:
database: other_db
The model that connects to another database is called User, but table it reference in other_db
is smf_users
, so my User.rb looks like this:
class User < ActiveRecord::Base
establish_connection "users_#{RAILS_ENV}"
set_table_name "smf_users"
end
During production I get this error:
Mysql::Error: Table 'production_db. smf_users' doesn't exist:
Notice how it is trying to connect to the wrong database and it is not finding the correct table. As I said, this works in development mode.
Any suggestions?
a source to share
I found that when using multiple databases, odd errors appear when creating associations. If you have another model that belongs_to :users
you expect it to work Just Work? Otherwise, you need to take a look at caching - Rails may not be able to properly cache the connection data for your secondary database.
a source to share
Try:
establish_connection configurations[RAILS_ENV]["users_#{RAILS_ENV}"]
User.connection
A hash of the connection information is required to establish a connection. It should return if from a .yml database.
You might want to check your log for the following message:
"Users_production database not configured"
This message will be called by ActiveRecord :: Base if it cannot find congifuration by line in database.yml.
a source to share
You may find this useful: http://magicmodels.rubyforge.org/magic_multi_connections/
a source to share