azback5 said over 3 years ago on User Notifications :
David, thanks for all of your help. 

I've been having trouble continuing to set this up with my nested Comments which belong to both a BlogPost & a Review.

For example, I only want a user to receive a notification if another User has commented on their comment. NOT on a BlostPost - which acts as a commentable and which  below each post are the nested comments.

What I've done is the following in my Comment model:

class Comment < ApplicationRecord



  belongs_to :commentable, polymorphic: true
  has_many :comments, as: :commentable
  belongs_to :user, optional: true

  acts_as_votable

  validates :body, presence: true

    def deleted?
      user.nil?
    end

    after_commit :create_notifications, on: :create

    private


  def create_notifications
   Notification.create do |notification|
     notification.notify_type = 'commentable'
     notification.actor = self.user
     notification.user = self.commentable.user
     notification.target = self
     notification.second_target = self.commentable
   end
 end

end



And I receive this error:

NoMethodError (undefined method `user' for #<BlogPost:0x00007fc3cab54fd8>
Did you mean?  super):
  
app/models/comment.rb:26:in `block in create_notifications'
app/models/comment.rb:23:in `create_notifications'
app/controllers/comments_controller.rb:17:in `create'


Which makes sense because a BlogPost doesn't belong to a User, so it can't find a user. So it is blocking the ability of a User to add a new comment beneath the BlogPost.

Is there a way I can write an argument like:

 if self.commentable = self
     notification.user = commentable.user
    end

Meaning if the commentable IS a comment and NOT a BlogPost, then the user to receive the notification is the user of the comment (and not incorrectly the BlogPost)

Your tutorial is very clear, but I'm hitting some weeds when I try to then implement this into the nested comments.

Hopefully this is making sense to you, and if not I'm happy to provide a link to my repo if needed.

Thanks for any help that you might be able to provide!

Cheers,

Aaron

(p.s. I've actually followed your previous nested comments tutorial and it is all set up and working great).