【发布时间】:2020-12-07 16:12:22
【问题描述】:
使用多态关联,我能够授予特定用户编辑特定帖子的权限。
我的模型是这样的
用户模型
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'
如何仅向通过 postusers 相关的用户发送邮件。
【问题讨论】:
-
顺便说一句:
has_many :postusers -
我不明白。
-
postuser 是单数,正如@benjessop 指出的那样,您可能希望将 has_many 更改为
:postusers。并在其他任何地方进行更改。 -
正如@benjessop 提到的,在用户模型和后期模型中都应该是
has_many :postures。因为post有很多postuser。当您致电@post.postusers时,您将获得所有姿势的数组。因此,您必须遍历姿势以获取单个用户` postusers = @post.postusers`,然后您可以遍历姿势 -
是您的模型名称
PostUser或Postuser吗?
标签: ruby-on-rails ruby mvcmailer