We can now commit this to git and push it to our repository as the initial state. Install all of the required gems that come with an initial Rails application. % rails new catalogue_cleanser -css tailwind -database=postgresql Also specify that TailwindCSS and Postgres are being used. Make sure to do this in the folder above where we cloned our GitHub repository. % gem install railsĬreate a new application. # => - current # =* - current & default # * - default I use rvm and take advantage of gemsets also. # reset config variables to original valuesĮNV = am in process of putting together a template for Rails applications with out of the box support for different database back-ends (PG, MySQL, Mongo, Arango.) within a Docker stack, basically a one liner to set up the whole thing, it will pop up soon on Github.The first step is to install Ruby. # set config variables for custom database # append and prepend proper tasks to all the tasks defined here above The constants come from initializers, eg: POSTGRES_DATABASE = YAML::load( ERB.new( File.read("#.rb"Īnd the rake task: lib/tasks/pg.rake desc "Managing the pg database" You can have two classes to subclass your models from: class PostgresRecord < ApplicationRecordĪnd class MysqlRecord < ApplicationRecord This also solved my next problem which would have been maintaining a pool of several hundred connections per type of model. Solved by putting models into sub-classes of ActiveRecord and managing connections from them sub-classes. 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? 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. dbconfig = YAML::load(File.open('./database.yml'))ĪctiveRecord::nfigurations = dbconfig Now create the new database for your Rails application using command: rake db:create This will create two databases with your Rails application's name. Press ESC and type :wq to save and close the file. I have done this instead of establishing a connection manually for each database. Set PostgreSQL user and its password in Rails application database configuration file Replace the username and its password with your own. Available: (ActiveRecord::AdapterNotSpecified 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. Then in the cron jobs: dbconfig = YAML::load(File.open('./database.yml'))ĪctiveRecord::Base.establish_connection(dbconfig) In the postgres-hosted models I do: class Thing < ActiveRecord::Base 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. This has worked for me after installing the pg gem: Rails: mysql & postgres at the same time in the same app? I do not want to move the whole database to Postgres. There are no relations between these models and any others. 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.
0 Comments
Leave a Reply. |