# Runtime pluggable models?

**URL:** <https://www.ruby-forum.com/t/runtime-pluggable-models/113662>\
**Category:** Rails\
**Created:** [September 3, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/runtime-pluggable-models/113662 "2007-09-03T16:46:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [September 3, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/runtime-pluggable-models/113662/1 "2007-09-03T16:46:48Z")

</div>

Hey list,

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

I’m looking into the possibility/feasibility of dynamically loading  
models  
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  
end

class CustomerAMyFoo \< AR  
include MyFooHelper  
end

class CustomerBMyFoo \< AR  
include MyFooHelper  
end

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,  
though  
I’m not particularly keen on it. Does another solution exist that  
doesn’t  
involve a proxy but without the overhead of reloading the module on each  
request?

Cheers  
Ian

---

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [September 3, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/runtime-pluggable-models/113662/2 "2007-09-03T19:50:27Z")

</div>

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

BUT

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?

---

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [September 4, 2007, 4:22am UTC](https://www.ruby-forum.com/t/runtime-pluggable-models/113662/3 "2007-09-04T04:22:42Z")

</div>

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.

Michael
