Reputation: 184
I am using sequel to query information from different databases. Using the following code
Sequel.connect(mmt018_db_connection_settings) do |db|
class ResultPlacementAssign < Sequel::Model(db[:result_placement_assign]); end
# do something with 'ResultPlacementAssign'
end
Sequel.connect(mmt024_db_connection_settings) do |db|
class PlatineMass < Sequel::Model(db[:platine_mass]); end
# do something with 'PlatineMass'
end
works great. But if I try to put my model definitions in separate files in order to require them when needed, it seems that only the first database connection works. So changing the above code to
Sequel.connect(mmt018_db_connection_settings) do |db|
require File.join(__dir__, 'models', 'm_mt_018', 'ResultPlacementAssign.rb')
# do something with 'ResultPlacementAssign'
end
Sequel.connect(mmt024_db_connection_settings) do |db|
require File.join(__dir__, 'models', 'm_mt_024', 'PlatineMass.rb')
# do something with 'PlatineMass'
end
using the model definitions
class ResultPlacementAssign < Sequel::Model(:result_placement_assign)
end
and
class PlatineMass < Sequel::Model(:platine_mass)
end
does not work.
It tells me that platine_mass
does not exist in database, because it's still using the first connection.
Am I missing something, or is there really no way to tell the model definition which database connection to use, if it's defined in a separate file.
Upvotes: 1
Views: 442
Reputation: 12149
You can tell Sequel which Database to use for a given model by passing a Database or Dataset argument to Sequel::Model()
. In general, you should assign Database objects to constants:
DB1 = Sequel.connect(mmt018_db_connection_settings)
DB2 = Sequel.connect(mmt024_db_connection_settings)
class ResultPlacementAssign < Sequel::Model(DB1[:result_placement_assign]); end
class PlatineMass < Sequel::Model(DB2[:platine_mass]); end
Upvotes: 3