# Multi applications on 1 database

**URL:** <https://www.ruby-forum.com/t/multi-applications-on-1-database/74325>\
**Category:** Rails\
**Created:** [November 4, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325 "2006-11-04T16:36:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [November 4, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325/1 "2006-11-04T16:36:27Z")

</div>

Hey,

I have a hosting where I can have only 1 database (shame on them!)  
On that hosting I want to run 2 application, but those application has  
the same models like

user.rb (table: users)  
role.rb

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [November 4, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325/2 "2006-11-04T16:40:26Z")

</div>

Hey,

I have a hosting where I can have only 1 database (shame on them!)  
On that hosting I want to run 2 application, but those application has  
the same models like

user.rb (table: users)  
role.rb (table: roles)

I have 2 Applcation A and B, and i want to run them at the same time. So  
i will do this sort of solution (not very handy)

For each application I when put an extra letter in the tables names

Appliction A: a\_users and a\_roles  
Appliction B: b\_users and b\_roles

This mean that I need to rename all me class to  
a\_user.rb a\_role.rb  
b\_user.rb b\_role.rb  
And everywhere i use it :s:s:s

Isnt there something where i can say per model the tablename  
Like:  
a\_user.rb tablename =\> a\_users  
a\_role.rb tablename =\> a\_roles  
b\_user.rb tablename =\> b\_users  
b\_role.rb tablename =\> b\_roles

Thanks

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [November 4, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325/3 "2006-11-04T18:14:30Z")

</div>

You can use table\_name\_prefix (part of your app’s config) to set a  
prefix for table names on a per application basis (see  
[http://glu.ttono.us/articles/2006/05/22/configuring-rails-environments-the-cheat-sheet](http://glu.ttono.us/articles/2006/05/22/configuring-rails-environments-the-cheat-sheet)),  
or you can use set\_table\_name if you want to change a model’s table name  
on a per-model basis.

Fred

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [November 4, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325/4 "2006-11-04T18:18:44Z")

</div>

Frederick C. wrote:

> You can use table\_name\_prefix (part of your app’s config) to set a  
> prefix for table names on a per application basis (see  
> [http://glu.ttono.us/articles/2006/05/22/configuring-rails-environments-the-cheat-sheet](http://glu.ttono.us/articles/2006/05/22/configuring-rails-environments-the-cheat-sheet)),  
> or you can use set\_table\_name if you want to change a model’s table name  
> on a per-model basis.
> 
> Fred

Hey,

I want to have each table this prefix =\> “d\_”

But where do i need to put this:

Rails::Initializer do |config|  
config.table\_name\_prefix = “d\_”  
end

Thanks

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325/5 "2006-11-07T11:01:33Z")

</div>

Brutyn N. wrote:

> i will do this sort of solution (not very handy)
> 
> For each application I when put an extra letter in the tables names
> 
> Appliction A: a\_users and a\_roles  
> Appliction B: b\_users and b\_roles
> 
> This mean that I need to rename all me class

Nah. First of all you can use ActiveRecord::Base.set\_table\_name to set  
the name of a models table regardless of the name of the model.

But perhaps even easier would be to use  
ActiveRecord::Base.table\_name\_prefix to set a global prefix for all your  
tables. Dunno if this is actually easier in your case.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [November 4, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325/6 "2006-11-04T19:04:51Z")

</div>

In your app’s config.rb

Fred

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325/7 "2006-11-07T11:01:45Z")

</div>

On 11/4/06, Brutyn N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey,
> 
> I have a hosting where I can have only 1 database (shame on them!)  
> On that hosting I want to run 2 application, but those application has  
> the same models like
> 
> user.rb (table: users)  
> role.rb

Hey, one problem with this is that if you’re using migrations (which  
you should be), the migrations will conflict. i.e. let’s say you run  
app a’s migrations, then app b will think the schema is up to that  
number, which may not be good for app b.

I wrote a plugin to take care of this by recording the schema info  
separately for each app you want to run.  
[http://svn.flpr.org/public/plugins/app\_migrations/README](http://svn.flpr.org/public/plugins/app_migrations/README)

Basically you just install the plugin, and in your environment.rb file  
add  
ActiveRecord::Base.app\_migration(:my\_app\_name)

Also if you set ActiveRecord::Base.table\_name\_prefix then all your  
tables will have a prefix so there won’t be naming conflicts. With  
all this set up you can develop with migrations and not worry about  
any kind of migration or naming conflicts.

hth

Pat

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [November 7, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/multi-applications-on-1-database/74325/8 "2006-11-07T19:38:38Z")

</div>

thanks that works
