# Dynamic table names

**URL:** <https://www.ruby-forum.com/t/dynamic-table-names/84636>\
**Category:** Rails\
**Created:** [February 6, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/dynamic-table-names/84636 "2007-02-06T17:20:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [February 6, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/dynamic-table-names/84636/1 "2007-02-06T17:20:40Z")

</div>

In my current project we’re using three databases and using joins  
inbetween them and when working on testing, it just did not seem to  
work to have multiple databases and using the set\_table\_name function  
since it hardcodes it to one database.

> From just looking around at some code, I copied and pasted this  
> together, which I will use to extend my models from (There are three  
> of them).

I could still use the one core database through the database.yml and  
only extend 2 of them.

Here is what I have. Example of one of the three.

In environment.rb I have  
PRODUCTS\_DATABASE = “products”

lib/products\_database.rb

module ARExtension  
def self.included(base) #:nodoc:  
base.extend(ClassMethods)  
end

module ClassMethods  
def ai\_table\_name(value)  
base\_table\_name = PRODUCTS\_DATABASE + “.” + value  
if RAILS\_ENV == “production”  
set\_table\_name(base\_table\_name)  
else  
set\_table\_name(base\_table\_name + “\_” + RAILS\_ENV)  
end  
end  
end  
end

class ProductsDatabase \< ActiveRecord::Base  
include ARExtension  
end

modules/testing.rb

class Testing \< ProductsDatabase  
ai\_table\_name “testings”  
belongs\_to :create\_user, :foreign\_key =\> “created\_by”, :class\_name  
=\> “User”  
belongs\_to :update\_user, :foreign\_key =\> “updated\_by”, :class\_name  
=\> “User”  
end

This will produce products.testings["","\_test","\_development] for the  
different RAILS\_ENV

I was just wondering if there is a better solution out there, perhaps  
something within rails to do what I need it to do.

Thanks,  
Fredrik

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [February 6, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/dynamic-table-names/84636/2 "2007-02-06T17:40:42Z")

</div>

Ok, I must have been a little confused this morning, That code would  
have to be modified to support.

products["","\_test","\_development].testings  
instead of  
products.testings["","\_test","\_development]

Thanks,  
Fredrik

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [February 6, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/dynamic-table-names/84636/3 "2007-02-06T17:44:42Z")

</div>

This should do it.

module ARExtension  
def self.included(base) #:nodoc:  
base.extend(ClassMethods)  
end

module ClassMethods  
def ai\_table\_name(value)  
if RAILS\_ENV == “production”  
set\_table\_name(PRODUCTS\_DATABASE + “.” + value)  
else  
set\_table\_name(PRODUCTS\_DATABASE + “\_” + RAILS\_ENV + “.” +  
value )  
end  
end  
end  
end

class ProductsDatabase \< ActiveRecord::Base  
include ARExtension  
end

Thanks,  
Fredrik
