I can send a comment to the author of the post with this function.How can I make a function that will send comments to posts for the last hour to the authors of these posts?
I am new to this business
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
CodePudding user response:
You can invoke job when create new post (in controller or somewhere)
NewPostCommentsCheckerJob.perform_later(post_id)
And define it like this
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
It will be invoked every hour, check new comments for last hour and call method of your mailer
Please also look here other ways to reschedule job: