Ruby on rails 如何向授权查看Ruby on Rails后多态关联的用户发送电子邮件
通过使用多态关联,我能够为特定用户提供编辑特定帖子的权限 我的模型看起来像这样 用户模型Ruby on rails 如何向授权查看Ruby on Rails后多态关联的用户发送电子邮件,ruby-on-rails,ruby,mvcmailer,Ruby On Rails,Ruby,Mvcmailer,通过使用多态关联,我能够为特定用户提供编辑特定帖子的权限 我的模型看起来像这样 用户模型 has_many :postuser has_many :posts, through: :postuser 后模型 has_many :postuser has_many :users, through: :postuser 邮政用户 belongs_to :post belongs_to :user 我想发送所有用户授权查看一篇文章时,文章是更新 在我的model_mailer中,我有 class
has_many :postuser
has_many :posts, through: :postuser
后模型
has_many :postuser
has_many :users, through: :postuser
邮政用户
belongs_to :post
belongs_to :user
我想发送所有用户授权查看一篇文章时,文章是更新
在我的model_mailer中,我有
class ModelMailer < ApplicationMailer
def new_user_notification(post)
@post = post
mail to: @Post.postuser.user.email, subject: "Welcome User"
end
end
如何仅向通过Postersers相关的用户发送邮件。这里有一个快速解决方案:
user.rb
has_many :postusers
has_many :posts, through: :postusers
post.rb
has_many :postusers
has_many :users, through: :postuser
姿势
belongs_to :post
belongs_to :user
after_commit -> {
ModelMailer.new_user_notification(post, user).deliver_now # or deliver_later
}, on: :create
model_mailer.rb
class ModelMailer < ApplicationMailer
def new_user_notification(post, user)
@post = post
mail to: user.email, subject: "Welcome User"
end
end
如果你想在帖子更新后发送电子邮件,你可以简单地执行以下操作
def new_record_notification(post, current_user_email)
@post = post
@current_user_email = current_user_email
users_email = post.postusers.map{|post| post.user.email}.join(",")
mail to: users_email, subject: "Your post has been updated"
end
然后,您可以在post控制器的更新方法中使用此选项
ModelMailer.new_record_notification(@project, current_user.email).deliver_now
顺便说一句:
has_many:postasers
我不明白。postaser是单数的,正如@benjessop所指出的,您可能想将has_many改为:postasers
。正如@benjessop提到的,无论是在用户模型中还是在后期模型中,都应该有很多:姿势。因为波斯特有很多姿势。当您调用@post.positioners
时,您将获得所有姿势的数组。因此,您必须循环使用姿势来获取单个用户`positioners=@post.positioners`,然后您可以循环使用您模型的positionresis名称positioner
或positioner
?
def new_record_notification(post, current_user_email)
@post = post
@current_user_email = current_user_email
users_email = post.postusers.map{|post| post.user.email}.join(",")
mail to: users_email, subject: "Your post has been updated"
end
ModelMailer.new_record_notification(@project, current_user.email).deliver_now