使用多态关联,我可以为特定用户提供编辑特定帖子的权限。
我的模特看起来像这样
用户模型
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
但我越来越
undefined method `postuser'
如何仅将邮件发送给通过postuser相关的用户。
如果您想在帖子更新后发送电子邮件,只需执行以下操作即可。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句