有人可以帮我弄清楚如何使用Rails编写以下sql(我正在使用Rails 4)Activerecord方法?我知道你可以用find_by_sql这样做,但我想保留活动记录关系.这是我正在尝试创建的postGresql数据库的sql:
SELECT * FROM (SELECT DISTINCT ON(table_a.id) table_a.name as alias_a,table_b.id,table_b.time FROM table_1 LEFT OUTER JOIN table_b ON table_a.id = table_b.id ORDER BY table_a.id,table_b.time asc) AS subquery ORDER BY alias_a asc
@subquery = table_a.select("DISTINCT ON(table_a.id) table_a.name as alias_a,table_b.time") @subquery = @subquery.joins("LEFT OUTER JOIN table_b ON table_a.id = table_b.id") @subquery = @subquery.order("table_a.id,table_b.time asc")
但是,我不知道如何编写一个使用@subquery作为外部select语句的表的select语句.