# One Model, multiple tables

**URL:** <https://www.ruby-forum.com/t/one-model-multiple-tables/67373>\
**Category:** Rails\
**Created:** [September 3, 2006, 1:21am UTC](https://www.ruby-forum.com/t/one-model-multiple-tables/67373 "2006-09-03T01:21:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bithai80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@bithai80](https://www.ruby-forum.com/u/bithai80)\
**Post date:** [September 3, 2006, 1:21am UTC](https://www.ruby-forum.com/t/one-model-multiple-tables/67373/1 "2006-09-03T01:21:29Z")

</div>

Hi,

Is there a clean way I could use one model and controller, but maps to  
different physical tables?

For example, I have a large photo site, broken down into type “A” and  
type “B” such as [siteA.mysite.com](http://siteA.mysite.com) and [siteB.mysite.com](http://siteB.mysite.com).

Lets assume few things first:

- 1 rails app
- I have two tables in database: siteA\_pictures and siteB\_pictures.
- I have two controllers one for each site to upload photo (code should  
be the same, maybe one controller should be shared in this case?)

I want a request to [siteA.mysite.com/upload\_photo](http://siteA.mysite.com/upload_photo) to be able to input  
data into the siteA\_pictures table and [siteB.mysite.com/upload\_photo](http://siteB.mysite.com/upload_photo)  
respectively.

So sharing same model code and perhaps controller code, but should input  
data into separate tables.

Thanks.

Truong.

---

<div class="post-metadata">

**Author:** ![bithai80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@bithai80](https://www.ruby-forum.com/u/bithai80)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/one-model-multiple-tables/67373/2 "2006-09-04T14:43:29Z")

</div>

> I want a request to [siteA.mysite.com/upload\_photo](http://siteA.mysite.com/upload_photo) to be able to input  
> data into the siteA\_pictures table and [siteB.mysite.com/upload\_photo](http://siteB.mysite.com/upload_photo)  
> respectively.  
> I think perhaps you need to question your database/system design. Would  
> it not be better to have one ‘pictures’ table and include a ‘site\_id’ in  
> the table? I imagine that with your current idea you’ll run in to more  
> problems that just this one.

You could have a site model that has many pictures (or articles or posts  
etc). Your code would be nice and simple then:

class Site \< ActiveRecord::Base  
has\_many :pictures  
end

class User \< ActiveRecord::Base  
belongs\_to :site  
end

And for the controller code:

site = Site.find(site\_id)  
site.pictures.find\_by\_name(“my\_dog.jpg”)

You can pick up the site id using the url ([siteb.mysite.com](http://siteb.mysite.com)) and only  
pictures from that site would show.

Hope that helps,

Steve

---

<div class="post-metadata">

**Author:** ![bithai80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@bithai80](https://www.ruby-forum.com/u/bithai80)\
**Post date:** [February 3, 2007, 5:43am UTC](https://www.ruby-forum.com/t/one-model-multiple-tables/67373/3 "2007-02-03T05:43:31Z")

</div>

hi there,

I perfectly understand what you’ve described, here’s more details and  
why I’m looking for a solution

- I have a legacy application that already has this in place (tables and  
stuff) and that application still uses this database
- say there are 500 customers and each customer has 4-5 special tables  
that have 10-50 millions rows

I understand this database desing is not perfect, I’m trying to come up  
with a quick and dirty temporary web app using rails and this database  
schema.

I found “Dr Nicâ€™s Magic Models” ([http://magicmodels.rubyforge.org/](http://magicmodels.rubyforge.org/)), but  
by some reason it doesn’t work (syntax errors etc.)

best,

–Alex

Stephen B. wrote:

> > I want a request to [siteA.mysite.com/upload\_photo](http://siteA.mysite.com/upload_photo) to be able to input  
> > data into the siteA\_pictures table and [siteB.mysite.com/upload\_photo](http://siteB.mysite.com/upload_photo)  
> > respectively.  
> > I think perhaps you need to question your database/system design. Would  
> > it not be better to have one ‘pictures’ table and include a ‘site\_id’ in  
> > the table? I imagine that with your current idea you’ll run in to more  
> > problems that just this one.
> 
> You could have a site model that has many pictures (or articles or posts  
> etc). Your code would be nice and simple then:
> 
> class Site \< ActiveRecord::Base  
> has\_many :pictures  
> end
> 
> class User \< ActiveRecord::Base  
> belongs\_to :site  
> end
> 
> And for the controller code:
> 
> site = Site.find(site\_id)  
> site.pictures.find\_by\_name(“my\_dog.jpg”)
> 
> You can pick up the site id using the url ([siteb.mysite.com](http://siteb.mysite.com)) and only  
> pictures from that site would show.
> 
> Hope that helps,
> 
> Steve

---

<div class="post-metadata">

**Author:** ![bithai80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@bithai80](https://www.ruby-forum.com/u/bithai80)\
**Post date:** [February 3, 2007, 11:45am UTC](https://www.ruby-forum.com/t/one-model-multiple-tables/67373/4 "2007-02-03T11:45:20Z")

</div>

On 2/3/07, Alex K… [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I understand this database desing is not perfect, I’m trying to come up  
> with a quick and dirty temporary web app using rails and this database  
> schema.
> 
> I found “Dr Nic’s Magic Models” ([http://magicmodels.rubyforge.org/](http://magicmodels.rubyforge.org/)), but  
> by some reason it doesn’t work (syntax errors etc.)

Perhaps you could use set\_table\_name in a before filter? e.g.:

def set\_customer\_tables  
customer\_id = find\_customer\_id  
Foo.set\_table\_name “foos\_#{customer\_id}”  
Bar.set\_table\_name “bars\_#{customer\_id}”  
# etc.  
end  
before\_filter :set\_customer\_tables

---

<div class="post-metadata">

**Author:** ![bithai80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@bithai80](https://www.ruby-forum.com/u/bithai80)\
**Post date:** [February 3, 2007, 8:10am UTC](https://www.ruby-forum.com/t/one-model-multiple-tables/67373/5 "2007-02-03T08:10:05Z")

</div>

Truong-an Thai wrote:

> Hi,
> 
> Is there a clean way I could use one model and controller, but maps to  
> different physical tables?
> 
> For example, I have a large photo site, broken down into type “A” and  
> type “B” such as [siteA.mysite.com](http://siteA.mysite.com) and [siteB.mysite.com](http://siteB.mysite.com).
> 
> Lets assume few things first:
> 
> - 1 rails app
> - I have two tables in database: siteA\_pictures and siteB\_pictures.
> - I have two controllers one for each site to upload photo (code should  
> be the same, maybe one controller should be shared in this case?)
> 
> I want a request to [siteA.mysite.com/upload\_photo](http://siteA.mysite.com/upload_photo) to be able to input  
> data into the siteA\_pictures table and [siteB.mysite.com/upload\_photo](http://siteB.mysite.com/upload_photo)  
> respectively.
> 
> So sharing same model code and perhaps controller code, but should input  
> data into separate tables.

Do you also want the same server instance to serve the siteA / siteB  
requests?  
If not you can put an entry in the file config/environment.rb

> Thanks.
> 
> Truong.
