# Multiple tables using one model?

**URL:** <https://www.ruby-forum.com/t/multiple-tables-using-one-model/130166>\
**Category:** Deployment\
**Created:** [February 26, 2008, 12:44am UTC](https://www.ruby-forum.com/t/multiple-tables-using-one-model/130166 "2008-02-26T00:44:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucasc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6a8cbe/32.png) [@lucasc](https://www.ruby-forum.com/u/lucasc)\
**Post date:** [February 26, 2008, 12:44am UTC](https://www.ruby-forum.com/t/multiple-tables-using-one-model/130166/1 "2008-02-26T00:44:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lucasc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6a8cbe/32.png) [@lucasc](https://www.ruby-forum.com/u/lucasc)\
**Post date:** [February 26, 2008, 1:00am UTC](https://www.ruby-forum.com/t/multiple-tables-using-one-model/130166/2 "2008-02-26T01:00:35Z")

</div>

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” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, February 25, 2008 3:44 PM  
Subject: [Rails-deploy] Multiple tables using one model?

---

<div class="post-metadata">

**Author:** ![lucasc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6a8cbe/32.png) [@lucasc](https://www.ruby-forum.com/u/lucasc)\
**Post date:** [February 26, 2008, 1:02am UTC](https://www.ruby-forum.com/t/multiple-tables-using-one-model/130166/3 "2008-02-26T01:02:56Z")

</div>

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  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lucasc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6a8cbe/32.png) [@lucasc](https://www.ruby-forum.com/u/lucasc)\
**Post date:** [August 18, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/multiple-tables-using-one-model/130166/4 "2008-08-18T22:10:55Z")

</div>

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.
