Direct connection to db

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?

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:

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 …”)?

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 :slight_smile:

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.

:slight_smile:


– Tom M.