# One model with lots of tables in Rails

**URL:** <https://www.ruby-forum.com/t/one-model-with-lots-of-tables-in-rails/152704>\
**Category:** Rails\
**Created:** [November 6, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/one-model-with-lots-of-tables-in-rails/152704 "2008-11-06T17:17:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@The\_N](https://www.ruby-forum.com/u/The_N)\
**Post date:** [November 6, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/one-model-with-lots-of-tables-in-rails/152704/1 "2008-11-06T17:17:41Z")

</div>

On a forum I discovered this. That was intended for to use on just one  
model:

module SharedStuff  
self.abstract\_class = true  
self.table\_name = ‘footable’  
self.table\_name = ‘bartable’  
end

class Foo \< ActiveRecord::Base  
include SharedStuff  
end

class Bar \< ActiveRecord::Base  
include SharedStuff  
end

Surely, this overrides the Rails conventions, but that’s what i intend  
to achieve. Can I do this in one model? Is this true? Can you verify  
this example take from here:  
[http://groups.google.com/group/rubyonrails-deployment/browse\_thread/thread/4244ed959648dc8a](http://groups.google.com/group/rubyonrails-deployment/browse_thread/thread/4244ed959648dc8a)  
?  
Hope you can help me.

Later

The N.

---

<div class="post-metadata">

**Author:** ![The\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@The\_N](https://www.ruby-forum.com/u/The_N)\
**Post date:** [November 6, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/one-model-with-lots-of-tables-in-rails/152704/2 "2008-11-06T20:16:28Z")

</div>

May I correct myself: Can I put all the tables of the database in one  
model? Making one-on-one when you have 49 tables is a royal PITA!  
Practical, but a PITA!

Hope I didn’t confuse you.

Later…

The N.

---

<div class="post-metadata">

**Author:** ![The\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@The\_N](https://www.ruby-forum.com/u/The_N)\
**Post date:** [November 7, 2008, 1:00am UTC](https://www.ruby-forum.com/t/one-model-with-lots-of-tables-in-rails/152704/3 "2008-11-07T01:00:52Z")

</div>

Your interpretation:

> module SharedStuff  
> self.abstract\_class = true  
> self.table\_name = ‘footable’  
> self.table\_name = ‘bartable’  
> end
> 
> class Foo \< ActiveRecord::Base  
> include SharedStuff  
> end
> 
> class Bar \< ActiveRecord::Base  
> include SharedStuff  
> end

isn’t the same as what is mentioned in that posting… including the  
SharedStuff module just makes those methods available to your model,  
each model should still spec its own table\_name. As you are set,  
everything reads from ‘bartable’ if I understand the internals  
correctly.

I prefer the abstract class method, and have a class GenericModel, which  
defines all the default behaviors for all the other models. Centralized  
cache management, polymorphic join management, all that ugly stuff you  
don’t want to type more than once.

Every other model inherits from GenericModel, overriding default  
behaviors if necessary. But GenericModel has no data fields, no table  
names, just methods.

I also use a GenericController, and for almost all of my other  
controllers, they contain nothing more than a before\_filter, sometimes a  
different layout, and any actions unique to their model. index, new,  
edit, save, update… they all reside in GenericController. Actions not  
done by all controllers, like deep copy, or PDF generation are in each  
controller that supports it.
