One model with lots of tables in Rails


#1

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
?
Hope you can help me.

Later

The N.


#2

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.


#3

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.