# Production vs Development I need to make different connections and call different code

**URL:** <https://www.ruby-forum.com/t/production-vs-development-i-need-to-make-different-connections-and-call-different-code/170300>\
**Category:** Rails\
**Created:** [June 15, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/production-vs-development-i-need-to-make-different-connections-and-call-different-code/170300 "2009-06-15T18:53:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [June 15, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/production-vs-development-i-need-to-make-different-connections-and-call-different-code/170300/1 "2009-06-15T18:53:45Z")

</div>

Bare with me on this one…

We have a lot of read only data. And it is accessed a lot more then  
the user created data at this stage. The datbase structure is (on  
production):

production\_user db

2 production\_read\_only databases behind HAProxy

So some models make a connection to the read\_only databases through  
HAproxy, which all works great. The issue is that some of the user  
created data that is stored in the production\_user db needs to be  
joined to the read only database at times.

So the scenario might be:  
You have 5 kinds of apples all stored in the read only database, they  
don’t change often.

But, a user comes and creates an account and picks his/her favorite  
apples and saves that config. On development you can use  
has\_and\_belongs\_to\_many for both the user and apples and it works  
great, all one database.

But, on production I need to store:

1. The user account on the production\_user db
2. the HABTM table on the user db because the read only databases are  
optimized for reads only and there are a lot of searches going on for  
apples apparently 🙂

But, the apples themselves are on the read only database. So if I say  
apples.users or users.apples i need to make connections to pull the  
interesting ides out of the corresponding tables, and make connections  
to the other databases in order to get my data.

I could use a lot of  
apple.users\_from\_production if RAILS\_ENV == ‘production’  
apple.users unless RAILS\_ENV == ‘production’  
all over the place, but that seems hokie.

Anyone else had to do this and what was your solution? Any help would  
be appreciated. Another solution would be helpful too, but the  
database structure is pretty well set. The readonly side needs to be  
very scalable and nodes need to be able to disappear and reappear  
based on load, hence haproxy so I can’t ‘just go to one db and use  
memcache everywhere’. 😃

Thanks.  
Erik

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [June 16, 2009, 2:14am UTC](https://www.ruby-forum.com/t/production-vs-development-i-need-to-make-different-connections-and-call-different-code/170300/2 "2009-06-16T02:14:36Z")

</div>

> 

Each model can use a different database connection. Assuming :readonly  
is a configuration defined in database.yml you can do

class Apple \< ActiveRecord::Base  
establish\_connection :readonly  
end

to have it use that connection
