0

我试图users在将它们添加到conversation. 我可以通过查找对话并简单地调用来访问所有用户@conversation.users。在这个例子中,它是 3。

conversation has_and_belongs_to_many :users
user has_and_belongs_to_many :conversations

在我的conversation.rb模型中,我尝试使用after_save.

  def send_notify_mail
    @conversation = self
    @conversation_users = @conversation.users
    @conversation_users.each do |user|
      ConversationMailer.notify_mail(self.id).deliver
    end
  end

我没有收到任何错误,但它只是将电子邮件发送给一个用户,而不是所有用户。我是否试图以正确的方式做到这一点?我应该遍历模型中的用户吗?

4

2 回答 2

0

这不应该

ConversationMailer.notify_mail(self.id).deliver

成为这个

ConversationMailer.notify_mail(user).deliver
于 2013-08-31T21:38:12.040 回答
0

The solution depends on how you've defined the notify_mail method. However, it's reasonable to assume that your code, as it stands, does not pass each user to the method – instead, it's passing the Conversation object. You can resolve this in the following manner:

@conversation_users.each do |user|
  ConversationMailer.notify_mail(user).deliver
end

Again, this is contingent on how your notify_mail method is structured, but if it's sending an email to the User object its being passed, then this would likely resolve your issue.

于 2013-08-31T21:45:50.373 回答