# Database connection per model

**URL:** <https://www.ruby-forum.com/t/database-connection-per-model/127858>\
**Category:** Rails\
**Created:** [February 1, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/database-connection-per-model/127858 "2008-02-01T21:48:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [February 1, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/database-connection-per-model/127858/1 "2008-02-01T21:48:56Z")

</div>

I’ve built database authentication into my app, however once they’re  
authenticated all their actions are done under the default user account  
setup in database.yml. I would like to establish a database connection  
in each model (so that all actions can be audited under the users’  
database accounts). My hang-up is trying to pass the session username  
and password into the establish\_connection call. I know the model can’t  
access session parameters, so was wondering if there’s another way. I  
know establishing database connection in the code isn’t best  
practice…so any other suggestions to accomplish this are also welcome.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [February 1, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/database-connection-per-model/127858/2 "2008-02-01T23:18:34Z")

</div>

On Feb 1, 8:48 pm, Brian P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I know the model can’t  
> access session parameters, so was wondering if there’s another way.

Maybe I’m being thick here, but why _can’t_ session parameters  
available in the controller be sent to the model to establish the  
connection?

I had models that have to change the database connection, depending on  
parameters passed to the controller action, so I’m mystified why that  
can’t happen for you! Mind you, sessions are something I have yet to  
tackle properly!

Allan

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [February 4, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/database-connection-per-model/127858/3 "2008-02-04T13:31:04Z")

</div>

Allan wrote:

> On Feb 1, 8:48 pm, Brian P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I know the model can’t  
> > access session parameters, so was wondering if there’s another way.
> 
> Maybe I’m being thick here, but why _can’t_ session parameters  
> available in the controller be sent to the model to establish the  
> connection?
> 
> I had models that have to change the database connection, depending on  
> parameters passed to the controller action, so I’m mystified why that  
> can’t happen for you! Mind you, sessions are something I have yet to  
> tackle properly!
> 
> Allan

Work  
around…[http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/](http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/)
