Notify owner of post and other commenters I have a rails4 app. When a user comments on a post it should send a notification to all the guys who commented on the post and to the post creator. I have a working method in the controller, but it's kinda ugly and I'd like to refactor it.

Could somebody tell me what the rails convention for refactoring this?

Comment belongs_to :post, post has_many :comments

create#comment action 

    if @post_comment.save
      ((@post.users + [@post.user]).uniq - [current_user]).each do |post_commenter|
        Notification.create(recipient_id: post_commenter.id, sender_id: current_user.id, notifiable: @post_comment.post, action: "commented")
    end
    ....
end
First of all I would add a method to Post:

# app/models/post.rb
def notify_others_about_new_comment(comment)
  recipients = [user]               # the author of the original post
  recipients &lt;&lt; users               # people that wrote a comment
  recipients.delete(comment.user)   # writer of the new comment

  recipients.uniq.each do |recipient|
    Notification.create(
      recipient_id: recipient.id, 
      sender_id: comment.user.id, 
      notifiable: self, 
      action: 'commented'
    )
  end
end


And just call that method in your controller:

if @post_comment.save
  @post.notify_others_about_new_comment(@post_comment)
  #...
end