投票后取消通知或评论自己的帖子?

时间:2015-03-27 07:47:38

标签: ruby-on-rails ruby ruby-on-rails-4

这是我的投票模型

  def send_vote_notification
    vote_type = self.vote ? 'love' : 'hate'
    article = ActionController::Base.helpers.link_to self.voteable.content[0..30], url_helpers.micropost_path(self.voteable), target: '_blank'
    Notification::Vote.create user_id: self.voteable.user_id, body: "a user #{vote_type} #{article}"
  end

这是我的评论模型

  def send_comment_notification

    article = ActionController::Base.helpers.link_to self.micropost.content[0..30], url_helpers.micropost_path(self.micropost), target: '_blank'

    Notification::Comment.create user_id: self.micropost.user_id, body: "a user add a comment for your article: #{article}"
  end

现在的问题是,如果他添加或投票自己的帖子,我想停止发送用户通知。我该怎么办?

1 个答案:

答案 0 :(得分:0)

在模型中

检查微博是否属于current_user

 def send_comment_notification
      article = ActionController::Base.helpers.link_to self.micropost.content[0..30], url_helpers.micropost_path(self.micropost), target: '_blank'
      Notification::Comment.create user_id: self.micropost.user_id, body: "a user add a comment for your article: #{article}" if self.micropost.user_id != self.id
 end