Runtime pluggable models?

Hey list,

My employers (and I) develop an application that is deployed for many
different businesses, and whilst we provide our functionality as a
hosted on our own servers, we are still required to integrated with
existing systems. This is starting to become a problem, as our generic
doesn’t quite fit nicely with all of our customers and consequentially
minor hackery has ensued.

I’m looking into the possibility/feasibility of dynamically loading
based on runtime conditions. I’m just wondering if anyone else has done
something similar? To be a little more specific, I imagine we’d need a
database table per customer (plus a default) and then a model that is
dynamically loaded in such a way that references to it throughout the
existing code remains the same. E.g:

class MyFoo < AR

class CustomerAMyFoo < AR
include MyFooHelper

class CustomerBMyFoo < AR
include MyFooHelper

All code, expect that contained in MyFoo, should not know that
CustomerAMyFoo and CustomerBMyFoo exist. I imagine MyFoo would act as a
simple proxy using some method_missing magic. That’s one approach,
I’m not particularly keen on it. Does another solution exist that
involve a proxy but without the overhead of reloading the module on each


I don’t understand why you want uniqe models for every customer?


I could imagine it will be little tricky to have different models files
for every request, and then again different database to use at the same
time, wouldnt it slow things down?

Why not provide plugin for the thing you are trying to do?

You can do what you are suggesting, and have them all direct
subclasses of AR. It might however be cleaner and easier to make
them subclasses of your AR subclass. This way the general aspects of
the application like associations can be on the shared superclass.
You can use single table inheritance if you expect little or no data
customization. If you expect a lot of data customization then you
can either use separate databases for each customer and set the
connection for each request, or you could use an abstract shared
superclass where each subclass gets its own table.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs