我需要邮件程序的合适功能
I need a suitable function from mailer
我可以用这个 post 向 post 的作者发送评论 我可以制作一个功能,将最后一小时的评论发送给 post 的作者吗这些 posts?
我是这个行业的新手
class CommentMailer < ApplicationMailer
def new_comment(comment)
@comment = comment
@post = @comment.post
mail to: @post.user.email, subject: "New comment on your post"
end
end
你可以在创建新的时候调用作业post(在控制器或某处)
NewPostCommentsCheckerJob.perform_later(post_id)
然后这样定义
class NewPostCommentsCheckerJob < ApplicationJob
RUN_EVERY = 1.hour
def perform(post_id)
Post.
find_by(id: post_id).
comments.
where(created_at: RUN_EVERY.ago..Time.current).
find_each do |new_comment|
CommentMailer.new_comment(new_comment).deliver_later
end
self.class.perform_later(post_id, wait: RUN_EVERY)
end
end
每小时调用一次,查看最近一小时的新评论和您的邮件程序的调用方法
另请查看此处其他重新安排工作的方法:
How do I schedule recurring jobs in Active Job (Rails 4.2)?
我可以用这个 post 向 post 的作者发送评论 我可以制作一个功能,将最后一小时的评论发送给 post 的作者吗这些 posts?
我是这个行业的新手
class CommentMailer < ApplicationMailer
def new_comment(comment)
@comment = comment
@post = @comment.post
mail to: @post.user.email, subject: "New comment on your post"
end
end
你可以在创建新的时候调用作业post(在控制器或某处)
NewPostCommentsCheckerJob.perform_later(post_id)
然后这样定义
class NewPostCommentsCheckerJob < ApplicationJob
RUN_EVERY = 1.hour
def perform(post_id)
Post.
find_by(id: post_id).
comments.
where(created_at: RUN_EVERY.ago..Time.current).
find_each do |new_comment|
CommentMailer.new_comment(new_comment).deliver_later
end
self.class.perform_later(post_id, wait: RUN_EVERY)
end
end
每小时调用一次,查看最近一小时的新评论和您的邮件程序的调用方法
另请查看此处其他重新安排工作的方法:
How do I schedule recurring jobs in Active Job (Rails 4.2)?