# Direct connection to db

**URL:** <https://www.ruby-forum.com/t/direct-connection-to-db/66089>\
**Category:** Rails\
**Created:** [August 23, 2006, 9:28am UTC](https://www.ruby-forum.com/t/direct-connection-to-db/66089 "2006-08-23T09:28:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [August 23, 2006, 9:28am UTC](https://www.ruby-forum.com/t/direct-connection-to-db/66089/1 "2006-08-23T09:28:26Z")

</div>

How do you make a direct connection to a db, not through the model?

Can I make use of execute “SELECT…”? Can that be put into a variable?

Not sure I’m thinking right here, but what if I want to fetch data  
connected to a user from many tables, say 10, should I use relations or  
is it smarter to fetch from each one separately?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [August 23, 2006, 10:46am UTC](https://www.ruby-forum.com/t/direct-connection-to-db/66089/2 "2006-08-23T10:46:57Z")

</div>

Look at the :include option in find. Rails can do your joins for you  
automatically.

On Aug 23, 2006, at 12:28 AM, Pål Bergström wrote:

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [August 23, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/direct-connection-to-db/66089/3 "2006-08-23T14:34:40Z")

</div>

Tom M. wrote:

> Look at the :include option in find. Rails can do your joins for you  
> automatically.
> 
> On Aug 23, 2006, at 12:28 AM, Pï¿½l Bergstrï¿½m wrote:

Is that as fast as doing a Model.find\_by\_sql(“SELECT …”)?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [August 23, 2006, 10:25am UTC](https://www.ruby-forum.com/t/direct-connection-to-db/66089/4 "2006-08-23T10:25:39Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> How do you make a direct connection to a db, not through the model?
> 
> Can I make use of execute “SELECT…”? Can that be put into a variable?
> 
> Not sure I’m thinking right here, but what if I want to fetch data  
> connected to a user from many tables, say 10, should I use relations or  
> is it smarter to fetch from each one separately?

Hi PÃ¥l,

Yes it is possible to connect directly to the db. You could create your  
own custom model, like my\_db.rb (accessed via MyDb.find etc) or you  
could use one of your existing models and just use  
Model.find\_by\_sql(“SELECT …”) or Model.find\_by\_sql([“SELECT…”,  
param1, param2, paramN…]).  
Using the last way, it’s possible to join those 10 tables of yours in 1  
select statement.

Hope that helps 🙂

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [August 23, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/direct-connection-to-db/66089/5 "2006-08-23T19:18:08Z")

</div>

On Aug 23, 2006, at 5:34 AM, PÃ¥l BergstrÃ¶m wrote:

> Tom M. wrote:
> 
> > Look at the :include option in find. Rails can do your joins for you  
> > automatically.
> > 
> > On Aug 23, 2006, at 12:28 AM, Pï¿½l Bergstrï¿½m wrote:
> 
> Is that as fast as doing a Model.find\_by\_sql(“SELECT …”)?

It’s actually _faster_ to write the code using the :include option,  
IMHO.

🙂

–  
– Tom M.
