Multiple tables using one model?

Is there a way to use one model to write to different tables if all of
the tables have the same fields and attributes?

I would like to use one model but be able to decide which table it
accesses.

Anyone know if this is possible?

Well, even if it were possible I wouldn’t advise it. The point of the
model
is to model your database, so each table you work with should have a
model.
I would say you’d have two models but have the logic in your method to
determine which model to interact with. Hope that helps a bit.

Raul

----- Original Message -----
From: “Lucas Campbell” [email protected]
To: [email protected]
Sent: Monday, February 25, 2008 3:44 PM
Subject: [Rails-deploy] Multiple tables using one model?

class AbstractBase < ActiveRecord::Base
self.abstract_class = true
end

class Foo < AbstractBase
self.table_name = ‘footable’
end

class Bar < AbstractBase
self.table_name = ‘bartable’
end

you may not need to set the table_name if the class names map to rails
conventions but just in case you need to do it that way.

Or;

module SharedStuff
all your common code amongst the models
end

class Foo < ActiveRecord::Base
include SharedStuff
end

class Bar < ActiveRecord::Base
include SharedStuff
end

either should achieve the same end.

James McCarthy
[email protected]

James Mccarthy wrote:

class AbstractBase < ActiveRecord::Base
self.abstract_class = true
end

class Foo < AbstractBase
self.table_name = ‘footable’
end

class Bar < AbstractBase
self.table_name = ‘bartable’
end

you may not need to set the table_name if the class names map to rails
conventions but just in case you need to do it that way.

Generating a separate model for each works best and you won’t need the
self.table_name=‘x’. The logic and associations can still all be in the
AbstractBase.