# Two databases in testing

**URL:** <https://www.ruby-forum.com/t/two-databases-in-testing/91460>\
**Category:** Rails\
**Created:** [April 9, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/two-databases-in-testing/91460 "2007-04-09T15:03:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@Karni](https://www.ruby-forum.com/u/Karni)\
**Post date:** [April 9, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/two-databases-in-testing/91460/1 "2007-04-09T15:03:18Z")

</div>

Hi Friends,

I am usiing the two databases in my project.  
But when i am writing the test cases,I am unable to specify the two  
databses,.

Can u give advice ,How can i specify the two databases in testing.

I hope i will get solution

Regards,  
-Karni

---

<div class="post-metadata">

**Author:** ![Karni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@Karni](https://www.ruby-forum.com/u/Karni)\
**Post date:** [April 9, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/two-databases-in-testing/91460/2 "2007-04-09T17:11:05Z")

</div>

Following is what I use for the same scenario (Better solutions  
welcome)  
I set one of these up for each database I use and use the appropriate  
class method for each model to tell it where it resides.

The XXX\_DATABASE is set in the environment.rb. Just contains the  
database name.

I don’t use database\_production either, the production database is  
simply named database.  
Also, my development database is also named database since it lives on  
a different server. Those terms are just too long for my taste.  
database\_prod and database\_dev would have been nicer.

# 

#If the table you are creating the model for  
#lives in the xxx database, make the model  
#extend XxxDatabase instead of ActiveRecord::Base  
#Also, when setting the table name (“must”), use xxx\_table\_name  
instead of set\_table\_name

# 

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

module ClassMethods

```
# use xxx_table_name "table_name" in your model
def xxx_table_name(value)
  if RAILS_ENV == "production" || RAILS_ENV == "development"
    set_table_name(XXX_DATABASE + "." + value)
  else
    #The test databases carry the _test at the end.
    set_table_name(XXX_DATABASE + "_" + RAILS_ENV + "." + value )
  end
end

```

end  
end

class XxxDatabase \< ActiveRecord::Base  
include ARExtension  
end

Let me know if does not make sense and I’ll explain further. When you  
run tests, these will be used and the appropriate databases will be  
used.

Fredrik

---

<div class="post-metadata">

**Author:** ![Karni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@Karni](https://www.ruby-forum.com/u/Karni)\
**Post date:** [April 9, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/two-databases-in-testing/91460/3 "2007-04-09T17:14:06Z")

</div>

Also, I put these files in /lib, named xxx\_database.rb

---

<div class="post-metadata">

**Author:** ![Karni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@Karni](https://www.ruby-forum.com/u/Karni)\
**Post date:** [April 11, 2007, 12:32pm UTC](https://www.ruby-forum.com/t/two-databases-in-testing/91460/4 "2007-04-11T12:32:38Z")

</div>

Hi Fredrik,

Very thankful to u …

I am new to ruby on rails. we have developed application in rails it is  
communicationg with PHP application.Everyhting is working fine.

I am using the two databases in the devlopment.(i.e one is PHP  
application database and other one is Ruby on rails application)

If u r not clear about my application i will give more information…

Please give more points use two databases in the testing

I hope u will give more clear

-Karni

Fredrik wrote:

> Following is what I use for the same scenario (Better solutions  
> welcome)  
> I set one of these up for each database I use and use the appropriate  
> class method for each model to tell it where it resides.
> 
> The XXX\_DATABASE is set in the environment.rb. Just contains the  
> database name.
> 
> I don’t use database\_production either, the production database is  
> simply named database.  
> Also, my development database is also named database since it lives on  
> a different server. Those terms are just too long for my taste.  
> database\_prod and database\_dev would have been nicer.
> 
> # 
> 
> #If the table you are creating the model for  
> #lives in the xxx database, make the model  
> #extend XxxDatabase instead of ActiveRecord::Base  
> #Also, when setting the table name (“must”), use xxx\_table\_name  
> instead of set\_table\_name
> 
> # 
> 
> module ARExtension  
> def self.included(base) #:nodoc:  
> base.extend(ClassMethods)  
> end
> 
> module ClassMethods
> 
> ```
> # use xxx_table_name "table_name" in your model
> def xxx_table_name(value)
> if RAILS_ENV == "production" || RAILS_ENV == "development"
> set_table_name(XXX_DATABASE + "." + value)
> else
> #The test databases carry the _test at the end.
> set_table_name(XXX_DATABASE + "_" + RAILS_ENV + "." + value )
> end
> end
> 
> ```
> 
> end  
> end
> 
> class XxxDatabase \< ActiveRecord::Base  
> include ARExtension  
> end
> 
> Let me know if does not make sense and I’ll explain further. When you  
> run tests, these will be used and the appropriate databases will be  
> used.
> 
> Fredrik
