关于如何根据关联向多个用户发送电子邮件的建议 [英] Advice on how to send email to multiple users based on an association

查看:163
本文介绍了关于如何根据关联向多个用户发送电子邮件的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Ruby on Rails应用程序,用户可以在其中跟踪锻炼。可以私下或公开地这样做。在公开的锻炼( workout.share == 1 )我允许用户评论。在锻炼时创建评论时,锻炼所有者将通过电子邮件通知。这一切都很好。

I have created a Ruby on Rails application where users can track workouts. The can do so either privately or publicly. On workouts which are public ( workout.share == 1 ) I allow users to comment. When a comment is created on a workout, the workout owner is notified via email. That all works great.

我现在正在寻找一些建议,最好的方法是允许已经评论过锻炼的用户,也可以通过电子邮件通知。这是一个例子。

I am now looking for some advice on the best way to allow users who have commented on a workout, to also be notified via email. Here is an example.

用户A创建锻炼1.用户B对锻炼1和用户A的评论收到电子邮件通知。用户C还对锻炼1进行评论,用户A和用户B均接收电子邮件通知。

User A creates Workout 1. User B comments on Workout 1 and User A receives an email notification. User C also comments on Workout 1 and both User A and User B receive email notifications.

告诉我的应用程序循环遍历所有的最佳方法是什么已经对锻炼1发表评论并向他们发送电子邮件的用户?

What is the best way to tell my application to loop through all the users who have commented on Workout 1 and send an email to them?

目前,我正在使用comments_controller中的以下代码向锻炼业主发送电子邮件(我知道这可能是更干净的代码):

Currently I am sending an email to the workout owner with the following code in the comments_controller (I realize this could be cleaner code):

class CommentsController < ApplicationController

...


def create
     @workout = Workout.find(params[:workout_id])
     @comment = @workout.comments.build(params[:comment])
     @comment.user = current_user

     respond_to do |format|
       if @comment.save
         if @comment.workout.email_notification == 1
          @comment.deliver_comment_notification_mail!
          format.html { redirect_to( projects_path) }
          format.js
        else
          format.html { redirect_to( projects_path) }
          format.js
        end
      else
      end
    end
  end

...

和comment_mailer.rb

and in comment_mailer.rb

def comment_notification_mail(comment)

     subject       "Someone commented on your Workout"
     recipients("#{comment.workout.user.username} <#{comment.workout.user.email}>")
     from("foobar")
     body         :comment => comment,
                  :commenter => comment.user,
                  :workout => comment.workout,
                  :commentee => comment.workout.user,
                  :workout_url => workout_url(comment.workout),
                  :commenter_url => user_url(comment.user)


   end


推荐答案

找出一个锻炼老板和评论者不是一个艰巨的任务。我的建议是:

To find out a workout owner and commenter is not a hard job. My suggestions are:


  1. 将您的控制器中发送电子邮件的代码移动到您的模型,使用 after_create ,例如:

class Comment < ActiveRecord::Base
  #...
  after_create :notify_subscribers


  def subscribers
    (self.workout.commenters << self.workout.owner).uniq
  end


  def notify_subscribers
    #... implemented below
  end
end


  • 使用 delayed_job 或其他工具将电子邮件发送作业设置为背景,或请求将被阻止,直到发送所有的电子邮件。例如,在 #notify_owner_and_commenter 方法

  • using delayed_job or other tools to put the email sending job to background, or the request would be blocked until all the emails has been sent. eg, in the #notify_owner_and_commenter method

    def notify_subscribers
      self.subscribers.each do |user|
        CommentMailer.send_later :deliver_comment_notification_mail!(self, user)
      end
    end
    

    然后,您需要用两个参数重构 #deliver_comment_notification_mail!方法。

    Then you need to refactor you #deliver_comment_notification_mail! method with two arguments.

    延迟工作参考: https://github.com/tobi/delayed_job

    这篇关于关于如何根据关联向多个用户发送电子邮件的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆