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 .. On Jun 19, 12:09 pm, Jedrin <jrubia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.
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.-- Posted via http://www.ruby-forum.com/.
On Jun 19, 5:44 pm, Chris Hanks <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> See for more information: > > http://www.fngtps.com/2008/11/free-result-after-using-activerecord-ba... > > 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. > > -- > Posted viahttp://www.ruby-forum.com/.