如何使用 join 和 group by 获取 rails 中最后输入的记录字段?

How to get the last entered record field in rails with join and group by?

我正在使用 rails 5.1 和 ruby 2.4,但我卡在 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      |

我认为是你的group_by。那是一个普通的 ruby 枚举。你试过.group('jobs.id')了吗? group 是一个 activerecord 查询助手,它将 运行 sql group by 子句

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("jobs.id, comments.id")