我想通过使用下面的sql获得查询结果:
select *
from (select id, account_uuid, type, content, reference_id, messages.created_at as msg_time
from messages
where content IS NOT NULL
order by created_at desc) a
inner join accounts
on accounts.uuid = a.account_uuid
group by a.account_uuid
order by a.id desc;
我在grids / messages.rb中编写了一个范围:
class Messages
include Datagrid
scope do
Account.group(:uuid).joins("select * from (select id, account_uuid, type, content, reference_id, messages.created_at as msg_time from messages where content IS NOT NULL order by created_at desc)")
.order("message_id desc")
end
end
我得到了sql语法错误(显然)。 请告诉我如何将上面的sql转换为范围的某些代码。非常感谢!
答案 0 :(得分:0)
我认为范围不适合您的代码使用。 为方便起见,最好使用类方法。