如何根据多态注释设置通知条件?

时间:2015-06-12 21:11:31

标签: ruby-on-rails ruby notifications comments polymorphic-associations

我在 comment.rb

中有这个
   def create_notification
      self.notifications.create(
        comment: self,
        goal: self.goal,
        valuation:  self.valuation,
        user: # How to fix this line?
          if valuation
            self.valuation.user,
          else goal
            self.goal.user,
          end           
        read: false
      )
   end

我们怎样才能将评论发布在通知使用user: self.valuation.user的评估时,并且当评论在目标时使用user: self.goal.user

如果您需要更多背景信息,请参阅上一个问题:How to send Notifications to the User whose post received a Comment?

1 个答案:

答案 0 :(得分:1)

我会写这样的东西:

def create_notification
  author = valuation ? valuation.user : goal.user
  notifications.create(
    comment:   self,
    goal:      goal,
    valuation: valuation,
    user:      author,          
    read:      false
  )
end