Forum: Ruby on Rails Multiple databases and fixtures problems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5dd240758e23e5e9c163a171cf096012?d=identicon&s=25 Matt Patterson (Guest)
on 2007-04-25 17:33
(Received via mailing list)
I'm working on a Rails app which makes use of two distinct (i.e. not
master-slave) databases, one ours and the other a central data store
which holds the core data we're using.

We have two sets of model classes, and the central store's models are
in a plugin and are namespaced (more than one project use them). So,
we have classes like:

  CentralStore::ModelName < ActiveRecord::Base

for the central store, and

  ModelName < ActiveRecord::Base

for ours. Our first big problem was with fixtures for the namespaced
models. I shimmed the ActiveRecord fixtures mechanism so you could
pass a block with the connection to the db you wanted the fixtures
created in and the right thing would be done:

  class TestClass < Test::Unit::TestCase
    fixtures(:model_one, :model_two) {CentralStore::Base.connection}

That's only mapping .yml files to DB table names though: The big
problem is that ActiveRecord's fixture helper methods look for class
names corresponding to table names (derived from the .yml files),
which means that no namespaced models will be found (table name ==
'model_name', class == 'CentralStore::ModelName').

The easy solution is to simply include the module in your
test_helper.rb. However, that leaves you open to the thing that bit me
today: if your central store's models gain a class with the same name
as one of yours (or vice versa).

Of course, the models are namespaced, so in application code there
isn't a problem. However, in tests, the models clash (you get a
superclass mismatch error and your tests just don't run...) and you
can't have fixtures for both tables.

My short term solution is to rename the local model, but has anyone
run into the same issue and done something elegant about it? I don't
want to rewrite the fixtures stack only to discover that someone else
has a less intrusive solution...

Thanks,

Matt
This topic is locked and can not be replied to.