如何根据多态评论设置通知条件?

How to set a notification conditional based on polymorphic comments?

我在 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

如果您需要更多上下文,请查看上一个问题:

我会这样写:

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