对于长别名,Rails复合组计数失败。我该如何解决?

时间:2018-08-16 14:32:12

标签: ruby-on-rails activerecord

我们正在尝试对对话中的邮件进行计数,并按user_id和session_id进行分组。似乎session_id + user_id列别名在64个字符后被截断。我需要在代码中进行哪些更改以避免这种截断,并帮助Rails正确解析结果集。由于列名被截断,所以Rails会忽略该列,并且不会生成正确的结果集。

这是我的代码:

replies = EmailMessage.joins(:email_conversation)
      .where(<irrelevant selectors>)
      .group('email_conversations.user_id, email_messages.email_conversation_id').count

然后我得到一个像这样的结果集:

  

{39092 => 1,43553 => 1,43555 => 3,...}

但是我期望这样的结果集:

  

{[39092,1234] => 1,[43553,1235] => 1,[43555,1236] => 3,...}

这是AR从初始代码段生成的查询:

  

选择COUNT(*)个count_all,email_conversations.user_id,   email_messages.email_conversation_id AS   email_conversations_user_id_email_messages_email_conversation_i从   “ email_messages” INNER JOIN“ email_conversations”开启   “ email_conversations”。“ id” =“ email_messages”。“ email_conversation_id”   在“ email_messages”中。“ sender_type” = $ 1并且   “ email_conversations”。“ email_domain_id” IN(选择   “ email_domains”。“ id”来自“ email_domains”在哪里   “ email_domains”。“ tenant_id” = $ 2)AND(“ email_messages”。“ created_at”   在$ 3和$ 4之间)GROUP BY email_conversations.user_id,   email_messages.email_conversation_id

我认为计数别名“ email_conversations_user_id_email_messages_email_conversation_i ”应为“ email_conversations_user_id_email_messages_email_conversation_id

运行查询时,得到以下结果集: enter image description here

数据看起来不错,但我怀疑AR不知道该列名应如何处理。

我需要对我的AR查询做些什么才能获得预期的结果集?

1 个答案:

答案 0 :(得分:1)

通过将它们作为单独的参数传递给let file = null; if (extension === 'pdf') { file = new Blob([new Uint8Array(byteFile)], { type: 'application/pdf' }); } else if (extension === 'jpg' || extension === 'png') { file = new Blob([new Uint8Array(byteFile)], { type: 'image/png' }); } else { file = new Blob([new Uint8Array(byteFile)], { type: 'application/msword' }); } if (file !== null) { const fileURL = URL.createObjectURL(file); window.open(fileURL, fileName); } ,告诉Rails它分为两列:

group