title: how to write raw sql in rails scope?
body: <p>I want to get a query result by using the sql below:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>and I wrote a scope in grids/messages.rb like this :</p>
  
  <pre><code>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
  </code></pre>
  
  <p>and I got the sql syntax error (obviously).
  Please tell me how to convert the sql above to some codes for scope. Thanks a lot!</p>