Home > Blockchain >  Initialize ActiveRecord model by passing database url
Initialize ActiveRecord model by passing database url

Time:08-03

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)

CodePudding user response:

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

  • Related