# Prepending table names on a per user or sub domain basis

**URL:** <https://www.ruby-forum.com/t/prepending-table-names-on-a-per-user-or-sub-domain-basis/63599>\
**Category:** Rails\
**Created:** [January 21, 2006, 1:33am UTC](https://www.ruby-forum.com/t/prepending-table-names-on-a-per-user-or-sub-domain-basis/63599 "2006-01-21T01:33:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 21, 2006, 1:33am UTC](https://www.ruby-forum.com/t/prepending-table-names-on-a-per-user-or-sub-domain-basis/63599/1 "2006-01-21T01:33:33Z")

</div>

Here is a sample of the database structure I want to have:

someclientname-websites  
someclientname-canned\_responses  
someclientname-canned\_responses\_websites  
anotherclientname-websites  
anotherclientname-canned\_responses  
anotherclientname-canned\_responses\_websites

As you can see I want to prepend the name of my future clients to the  
table name so that they use completely different sets of data.

I know I can do the actual prepend using the Rails config files but how  
do I set it on a per user basis (or on a per sub-domain basis)?

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 21, 2006, 10:05am UTC](https://www.ruby-forum.com/t/prepending-table-names-on-a-per-user-or-sub-domain-basis/63599/2 "2006-01-21T10:05:52Z")

</div>

Per,

Some coding for this is required, to dynamically come to the value of  
the  
variable (if possible because I’m a newbie), but there’s something  
called  
“set\_table\_name” that let’s you differ the table name from the default  
naming  
convention. It may look somewhat abusive but it should work. See:

[http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas](http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas)

And auto pluralize can be turned of (to get more grip on the naming)  
with:

ActiveRecord::Base.pluralize\_table\_names = false

Hope it helps.

Regards,

Gerard.

On Saturday 21 January 2006 01:33, Per Djurner tried to type something  
like:

> table name so that they use completely different sets of data.
> 
> I know I can do the actual prepend using the Rails config files but how  
> do I set it on a per user basis (or on a per sub-domain basis)?

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 21, 2006, 1:05pm UTC](https://www.ruby-forum.com/t/prepending-table-names-on-a-per-user-or-sub-domain-basis/63599/3 "2006-01-21T13:05:30Z")

</div>

> Some coding for this is required, to dynamically come to the value of  
> the  
> variable (if possible because I’m a newbie), but there’s something  
> called  
> “set\_table\_name” that let’s you differ the table name from the default  
> naming  
> convention.

Thanks for responding.

The problem is (or at least one of the problems) is that the request  
variables are not available in models. So this does not work:

set\_table\_name “#{@request.subdomains.first}\_websites”

Any more ideas anyone?

if no one else has managed to do this or if it’s impossible I guess I  
will have to go back to having the data in the same tables for all  
acounts and just using account\_ids as foreign keys in all other tables,  
would like to avoid this if possible though.

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 21, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/prepending-table-names-on-a-per-user-or-sub-domain-basis/63599/4 "2006-01-21T14:13:50Z")

</div>

Per,

> Any more ideas anyone?  
> Are the tables created during runtime (so if a user creates an account),  
> because then executing sql with the correct table names might be an  
> idea.

> if no one else has managed to do this or if it’s impossible I guess I  
> will have to go back to having the data in the same tables for all  
> acounts and just using account\_ids as foreign keys in all other tables,  
> would like to avoid this if possible though.  
> Why is that? From a DBA’s point of view (not that I am one … 😉 Aren’t  
> a few  
> tables easier to manage?

Gerard

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 21, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/prepending-table-names-on-a-per-user-or-sub-domain-basis/63599/5 "2006-01-21T15:23:18Z")

</div>

> Why is that? From a DBA’s point of view (not that I am one … 😉 Aren’t  
> a few  
> tables easier to manage?

I knew that question was gonna come up sooner or later 😉

2 reasons:

1. I want to have that extra security added so that no matter how stupid  
I code the application no user of one account will ever see anyone elses  
data (in case I do a find instead of a find\_by\_account\_id somewhere for  
example).

2. the app itself will be much easier to code. For example given my db  
structure of (Account has many Websites which in turn has many Website  
Domains) I can retrieve all website domains for one account simply by  
doing WebsiteDomains.find\_all instead of a more complex query with  
joins/where clauses etc that would otherwise be needed to get all the  
relations right from account down to website domain.

/ Per
