Implications of the connection attribute
yujiri8 opened this issue · 3 comments
Since the connection
attribute is defined on models, is it possible to use a model with multiple databases at once; for example a production and testing database? And would it be possible to use multiple models within a transaction, since it seems based on #379 the transaction feature is accessed from a model?
is it possible to use a model with multiple databases at once
Not the way you're thinking at least. You would be able to register the connection differently based on ENV
vars for example. I.e. parameterize Granite::Connections << Granite::Adapter::Mysql.new("mysql", "YOUR_DATABASE_URL")
would it be possible to use multiple models within a transaction
I think so yea. The transaction instance is obtained from a model's adapter. IMO this isn't ideal but it is what it is. I'm doing just that for another project of mine. Saving three objects within a transactions using save!
. If one of them fails to save the previous one's don't get committed.
Okay, thanks. I guess I'll close this now.
By using a model with multiple databases I meant at the same time, not just with the same code. But I guess that could probably be accomplished by subclassing the model and just overriding the connection?
with multiple databases I meant at the same time
Ah no, a model at the moment can only be associated with a single connection at a time
But I guess that could probably be accomplished by subclassing the model and just overriding the connection?
I don't actually think subclasses would work, you'd run into #123. Maybe distinct types using modules to include
the properties?