SQL in rails without active record?

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.

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

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

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.

On Jun 19, 5:44 pm, Chris H. [email protected]
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