rails 电子邮件通知 - 邮件程序

rails email notification- mailer

我是 rails 的新手。用户需要在几天后收到电子邮件通知,前提是他们还没有阅读通知。我已经使用 Action Mailer 创建了电子邮件通知,但在添加时间格式后它不起作用。邮件在 5 天后仍未发送给用户。

class NotificationsController < ApplicationController

 def show
  @notification = current_user.notifications.find_by_id(params[:id])

  if current_user.notifications.unread > 5.days.ago
   UserMailer.try_notif(@notification, @post, @user).deliver
  end
 end

 def update
  @notification = current_user.notifications.find_by_id(params[:id])
  @notification.update(read: true)
  redirect_to post_path(@notification.post)
 end

 def read_all
  current_user.notifications.unread.update_all(read: true)
 end
end

user_mailer.rb

class UserMailer < ApplicationMailer

 def try_notif(notification, post, user)
  @notification = notification
  @post = post
  @user = user
  mail(to: @post.user.email, subject: 'New Notification')
 end
end

我认为正确的代码应该是这样的: 需要用当前时间检查每个通知

if current_user.notifications.unread
  current_user.notifications.each do |notification|
   if Time.current - notification.created_at > 120  #just checking in hours
    #send reminder
   end
  end
end

或者可以尝试其他方式

@notifications = Notification.where(created_at: 5.days.ago..Time.current, unread: true)
if @notifications 
  @notifications.each do |notification|
   #send notification
  end
end