One Model, multiple tables

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 and 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 to be able to input
data into the siteA_pictures table and siteB.mysite.com/upload_photo
respectively.

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

Thanks.

Truong.

I want a request to siteA.mysite.com/upload_photo to be able to input
data into the siteA_pictures table and 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) and only
pictures from that site would show.

Hope that helps,

Steve

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/), 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 to be able to input
data into the siteA_pictures table and 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) and only
pictures from that site would show.

Hope that helps,

Steve

On 2/3/07, Alex K… [email protected] 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/), 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

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 and 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 to be able to input
data into the siteA_pictures table and 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.