【问题标题】:how to write raw sql in rails scope?如何在 Rails 范围内编写原始 sql?
【发布时间】:2023-03-25 00:30:01
【问题描述】:

我想通过下面的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 转换为一些范围代码。非常感谢!

【问题讨论】:

    标签: sql ruby-on-rails datagrid scope


    【解决方案1】:

    我认为在此处为您的代码使用范围不是正确的。 为方便起见,您最好使用类方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多