I have two databases in SQLite with the same schema. The other
database was populated by an external
customer and then returned. I want to use ActiveRecord to do the
comparison.
I am not sure of the best way to access the tables in each database
with ActiveRecord models. I could
change the connection dynamically whenever I want to switch between
the databases, but this would seem to be clumsy and error prone.
Another approach would be to create a 2nd set of classes at runtime
which cloned the original ActiveRecord
model classes and inherits from a different base class with a
different connection.
If anyone has done anything like this or has any suggestions on the
best course of action, I would appreciate it.
Honestly, were it me, I would just copy the model files, rename them to something like ModelOtherDB, and use establish_connection and set_table_name.
Might be somewhat clunky, and might not be the route to go if this is something you’re going to need to abstract to multiple applications, but if it’s just the one, then that should probably work fine.
Luke,
Thanks for the quick response. It is something that I will probably
need to
abstract for future application. I believe your suggestion is good, I
would
probably something similar at runtime if possible to create the
additional classes and
connection. Thanks for your comment on 'set_table_name', I forgot
about
that completely.
I have two databases in SQLite with the same schema. The other
database was populated by an external
customer and then returned. I want to use ActiveRecord to do the
comparison.
Read /Rails Recipes/; it shows how to open two databases at the same time.
(But seriously, if this is a one-shot with sqlite3, I would just .dump
the databases and diff them...