AnthonyGalli.com
AnthonyGalli.com

Reputation: 2866

How to send Notifications to the User whose post received a Comment?

With my current code the user who made the comment gets a notification telling them that they made a comment.

It should be the person who posted the Valuation that got commented on who should get a notification notifying them that someone commented on it.

Standard notification stuff, but where did I go wrong?!

comment.rb

class Comment < ActiveRecord::Base
  after_create :create_notification
  has_many :notifications
  has_many :comment_likes   
  has_many :likers, through: :comment_likes, class_name: 'User', source: :liker
  belongs_to :habit
  belongs_to :quantified
  belongs_to :valuation
  belongs_to :goal
  belongs_to :user
  validates :user, presence: true

private

  def create_notification # This is definitely the issue I made a lot of attempts to fix it, but now it's even more confusing
    @valuation = Valuation.find_by(self.valuation_id)
    @comment = Comment.find_by(self.id)
    @user = User.find_by(@comment.user_id).id
      self.notifications.create(
        comment: self,
        valuation: self.valuation,
        user: self.user,
        read: false
      )
  end
end

notification.rb

class Notification < ActiveRecord::Base
  belongs_to :comment
  belongs_to :valuation
  belongs_to :user
end

notifications_controller.rb

  def index
    @notifications = current_user.notifications
    @notifications.each do |notification|
      notification.update_attribute(:read, true) 
    end
  end

notifications/index

<%= link_to notification.user_id, user_path(Comment.find_by(notification.comment_id).user.id) %> commented on <%= link_to "your value", notification_valuation_path(notification, notification.valuation_id) %>

valuation.rb

class Valuation < ActiveRecord::Base
    belongs_to :user
    has_many :activities
  acts_as_taggable
  has_many :combine_tags
  has_many :notifications
  validates :name, presence: true
    has_many :notes, as: :notable
    scope :publish, ->{ where(:conceal => false) }
    has_many :notes
  has_many :valuation_likes 
  has_many :likers, through: :valuation_likes, class_name: 'User', source: :liker
  has_many :comment_likes
    has_many :comments

  scope :randomize, -> do
      order('RANDOM()').
      take(1)
    end

    scope :top_25, -> do
      order('RANDOM()').
      limit(25)
    end
end

Please let me know if you need further explanation or code to help you help me :-]

Link is a tutorial I used.

Upvotes: 0

Views: 374

Answers (2)

Pardeep Dhingra
Pardeep Dhingra

Reputation: 3946

Try this:

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

Notification Link:

   <%= link_to notification.comment.user_id, user_path(notification.comment.user_id) %> commented on <%= link_to "your value", notification_valuation_path(notification, notification.valuation_id) %>      

Upvotes: 1

Aakash Aggarwal
Aakash Aggarwal

Reputation: 306

I think you are looking for this:

def create_notification
    @valuation = Valuation.find_by(self.valuation_id)
    @comment = Comment.find_by(self.id)
    @user = User.find_by(@valuation.user_id).id
      self.notifications.create(
        comment: self,
        valuation: self.valuation,
        user:@valuation.user,
        read: false
      )
  end

For the notification index try:

<%= link_to Comment.find_by(notification.comment_id).user.id, user_path(Comment.find_by(notification.comment_id).user.id) %> commented on <%= link_to "your value", notification_valuation_path(notification, notification.valuation_id) %>

Upvotes: 2

Related Questions