Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Initialize ActiveRecord model by passing database url

i know that active record rails can have multiple connection to be specified in model. we can do something like this right?

class Car < ApplicationRecord
 establish_connection("url_to_database")
end

but how to pass the url when initialize the model dynamically?
is it possible to do something like this below?

Car.new(url: url)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This should work:

Car.establish_connection(url)

Please note that this database connection is per model not per instance of that model.

See ConnectionHandling#establish_connection

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading