Nested queries in Arel
Posted
by Schrockwell
on Stack Overflow
See other posts from Stack Overflow
or by Schrockwell
Published on 2010-05-24T04:57:03Z
Indexed on
2010/05/24
5:00 UTC
Read the original article
Hit count: 457
ruby-on-rails
|arel
I am attempting to nest SELECT queries in Arel and/or Active Record in Rails 3 to generate the following SQL statement.
SELECT sorted.* FROM (SELECT * FROM points ORDER BY points.timestamp DESC) AS sorted GROUP BY sorted.client_id
An alias for the subquery can be created by doing
points = Table(:points)
sorted = points.order('timestamp DESC').alias
but then I'm stuck as how to pass it into the parent query (short of calling #to_sql
, which sounds pretty ugly).
How do you use a SELECT statement as a sub-query in Arel (or Active Record) to accomplish the above? Maybe there's an altogether different way to accomplish this query that doesn't use nested queries?
© Stack Overflow or respective owner