I've built a Rails application using MySQL and now would like to move some models to a Postgres database for performance reasons, the tables are quite big and Postgres is faster.
There are no relations between these models and any others. I do not want to move the whole database to Postgres.
This has worked for me after installing the pg gem: Rails: mysql & postgres at the same time in the same app?
Now I have a problem: some cron jobs which load the same database.yml file stopped working because of those models that use the postgres database although the YML is ok, indented fine, reads fine, the "postgres" section is there, the Rails application works fine.
In the postgres-hosted models I do:
class Thing < ActiveRecord::Base
establish_connection(:postgres)
self.table_name = "thingies"
end
Then in the cron jobs:
dbconfig = YAML::load(File.open('../database.yml'))
ActiveRecord::Base.establish_connection(dbconfig["production"])
ActiveRecord::Base.establish_connection(dbconfig["postgres"])
Then when I load one of the postgres models I get this error:
/usr/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection': 'postgres' database is not configured. Available: [] (ActiveRecord::AdapterNotSpecified
How can I solve this?
Later edit: I have done this instead of establishing a connection manually for each database.
dbconfig = YAML::load(File.open('../database.yml'))
ActiveRecord::Base.configurations = dbconfig
Now the errors above are gone, but the MySQL models throw errors as it appears ActiveRecord establishes the connection to postgres and leaves it open.
Is there a way to specify what connection to use for the models that don't specify what connection to use? Sorts of a default?