2

ruby 2.4 で Rails 5.1 を使用していますが、Rails のアクティブ レコード クエリでスタックしています。これは既に SQL で作成していますが、Rails の方法で複製することはできません。正しい構文を提案してください。事前に感謝します。Sql クエリは次のとおりです。

select jobs.id, users.first_name, users.last_name, users.email, MAX(comments.created_at) from comments left join jobs on jobs.id = comments.job_id left join users on users.id = jobs.user_id group by jobs.id;

Railsで作成された対応するクエリは次のとおりです。

Comment.joins("left join jobs on jobs.id = comments.job_id").joins("left join users on users.id = jobs.user_id").select("jobs.id, users.first_name, users.last_name, users.email, max(comments.created_at)").group_by("jobs.id")

次のエラーが発生しています:

Completed 500 Internal Server Error in 151ms (ActiveRecord: 6.1ms)

ArgumentError (wrong number of arguments (given 1, expected 0)):

出力は次のようになります。

+-------+------------+-----------+------------------------+--------------------------+
| id    | first_name | last_name | email                  | MAX(comments.created_at) |
+-------+------------+-----------+------------------------+--------------------------+
| 63606 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-18 05:19:30      |
| 63608 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-26 09:22:21      |
| 63610 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 12:27:24      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 12:15:36      |
| 63684 | vidur      | punj      | vidur.punj@hotmail.com | 2019-10-15 11:46:45      |
| 63690 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-12 05:29:04      |
| 63694 | vidur      | punj      | vidur.punj@hotmail.com | 2019-05-23 04:03:01      |
| 63700 | vidur      | punj      | vidur.punj@hotmail.com | 2019-05-20 10:55:31      |
| 63701 | vidur      | punj      | vidur.punj@hotmail.com | 2019-10-19 10:07:42      |
| 63704 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-13 09:52:50      |
| 63705 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-11 11:16:35      |
| 63706 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-11 11:15:21      |
| 63708 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-13 12:44:19      |
| 63709 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-11 11:13:48      |
| 63717 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-17 09:42:35      |
| 63719 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-17 10:38:51      |
| 63726 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-20 10:55:44      |
| 63727 | vidur      | punj      | vidur.punj@hotmail.com | 2019-06-25 04:57:45      |
| 63741 | vidur      | punj      | vidur.punj@hotmail.com | 2019-07-22 06:51:53      |
| 63778 | vidur      | punj      | vidur.punj@hotmail.com | 2019-09-13 11:30:56      |
| 63801 | vidur      | punj      | vidur.punj@hotmail.com | 2019-09-16 06:55:08      |
| 63802 | vidur      | punj      | vidur.punj@hotmail.com | 2019-09-17 03:56:27      |
| 63815 | vidur      | punj      | vidur.punj@hotmail.com | 2019-10-30 05:43:07      |
| 63821 | vidur      | punj      | vidur.punj@hotmail.com | 2019-10-15 10:40:54      |
| 63839 | vidur      | punj      | vidur.punj@hotmail.com | 2019-09-23 11:07:30      |
| 63898 | vidur      | punj      | vidur.punj@hotmail.com | 2019-10-29 07:12:15      |
| 63916 | vidur      | punj      | vidur.punj@hotmail.com | 2019-10-15 12:51:06      |
+-------+------------+-----------+------------------------+--------------------------+

それ以外の:

+-------+------------+-----------+------------------------+--------------------------+
| id    | first_name | last_name | email                  | max(comments.created_at) |
+-------+------------+-----------+------------------------+--------------------------+
| 63606 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-18 05:18:27      |
| 63606 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-18 05:19:30      |
| 63608 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-18 11:06:48      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 05:33:57      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:04:16      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:49:08      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:49:49      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:50:41      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:51:27      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:52:17      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:52:48      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:53:31      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:53:46      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:54:07      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:54:27      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:54:45      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:55:28      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 06:58:00      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 08:41:57      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 09:08:07      |
| 63611 | vidur      | punj      | vidur.punj@hotmail.com | 2019-04-22 09:08:34      |
4

2 に答える 2