【发布时间】:2017-11-30 12:36:38
【问题描述】:
我有以下疑问:
我有以下型号:
2.1.5 :025 > OnlineCourseRegistration.last
=> #<OnlineCourseRegistration id: 14392, cart_id: 15177, user_id: 7133, course_class_id: 681, created_at: "2017-10-29 23:28:45", updated_at: "2017-10-30 20:18:53", exam_attempts: 0, exam_completed_at: nil, evaluation_completed_at: nil, status: "Active", score: "", add_extension: false, retest_cart_id: nil, retest_purchased_at: nil>
...我正在运行这个查询:
registrations = OnlineCourseRegistration.where(course_class_id: 123).where(status: "Completed").where("score >= ?", 80)
可能会返回多个相同 user_id 的记录。如果是这种情况,我只想返回最后一条记录...或该用户的最新 :exam_completed_at 日期的记录。
这里的上下文是整个循环:
registrations = OnlineCourseRegistration.where(course_class_id: 123).where(status: "Completed").where("score???? >= ?", 80)
if !registrations.empty?
registrations.each do |b|
email_recipients << b
end
我建立 email_recipients 数组,然后将其交给邮件程序。我遇到的问题是,如果用户被退回两次,那么他们会收到两封电子邮件。我只希望他们收到一封电子邮件,所以我想在返回的最后一条记录(或最近的exam_completed_at 日期的记录。
【问题讨论】:
-
您可以在电子邮件数组上附加
order(exam_completed_at: :desc).select('distinct on (email) name')或直接调用.uniq...
标签: ruby-on-rails