# SQL in rails without active record?

**URL:** <https://www.ruby-forum.com/t/sql-in-rails-without-active-record/170611>\
**Category:** Rails\
**Created:** [June 19, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/sql-in-rails-without-active-record/170611 "2009-06-19T18:10:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 19, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/sql-in-rails-without-active-record/170611/1 "2009-06-19T18:10:39Z")

</div>

How can I do sql inside of rails where active record is not  
involved ? I have done find\_by\_sql type things before, but they always  
return an active record. There may be a need on this project to do a  
more typical sql select that does a join etc.

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 19, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/sql-in-rails-without-active-record/170611/2 "2009-06-19T18:38:34Z")

</div>

I think I figured this out, it turned out to be fairly easy …

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 19, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/sql-in-rails-without-active-record/170611/3 "2009-06-19T18:44:05Z")

</div>

You might try

ActiveRecord::Base.connection.execute “SELECT…”

Or, in a model file, which is where you probably should be doing this  
kind of thing, I guess it would just be connection.execute “(query)”

See for more information:

[http://www.fngtps.com/2008/11/free-result-after-using-activerecord-base-connection-execute](http://www.fngtps.com/2008/11/free-result-after-using-activerecord-base-connection-execute)

If you just need to do a join, though, you can pass the :join option to  
active record in whatever query you’re doing.

Jedrin wrote:

> How can I do sql inside of rails where active record is not  
> involved ? I have done find\_by\_sql type things before, but they always  
> return an active record. There may be a need on this project to do a  
> more typical sql select that does a join etc.

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 19, 2009, 9:19pm UTC](https://www.ruby-forum.com/t/sql-in-rails-without-active-record/170611/4 "2009-06-19T21:19:25Z")

</div>

On Jun 19, 5:44 pm, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You might try
> 
> ActiveRecord::Base.connection.execute “SELECT…”
> 
> Or, in a model file, which is where you probably should be doing this  
> kind of thing, I guess it would just be connection.execute “(query)”

select\_all returns a more portable result (array of hashes rather than  
something db specific)

Fred
