How to set a notification conditional based on polymorphic comments?

118 views Asked by At

I have this in 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

How can we make it where when the comment is on a valuation the notification uses user: self.valuation.user and when the comment is on a goal it uses user: self.goal.user?

Here's previous question if you need more context: How to send Notifications to the User whose post received a Comment?

1

There are 1 answers

0
spickermann On BEST ANSWER

I would write something like this:

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