Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create notification system in rails?

Basically, I want to create a notification like Facebook and Stackoverflow. Specifically, in a Post-Comments system, when a post get commented, everyone involved (people who creates the post and who create comments, except the new commenter) gets a notification message that this post get commented. And the notification get dismissed when people have read it.

I have tried to use mailboxer gem to implement it, but saddly there is no example available using its related methods, including social_stream itself.

Is there other way to create the Notification System?

And when I try to create it from scratch I get several problems:

    Model Notification
    topic_id: integer
    user_id: integer
    checked: boolean #so we can tell whether the notification is read or not
  1. Dismissing the notication after being read by users

I think we just need to turn every notification messages' "checked" attribute to true after user visit the index of notification.(In the NotificationsController)

    def index
      @notifications=current_user.notication.all
      @notification.each do |notification|
         notification.checked = true
      end
      @notification.save!
    end

2.Selecting users to notify(and exclude the user making new comment)

I just have no idea in wrting queries....

3.Creating notifications

I think this should be something like

    #in CommentController
    def create
      #after creating comments, creat notifications
      @users.each do |user|
        Notification.create(topic_id:@topic, user_id: user.id)
      end
    end

But I think this is really ugly

There is no need to anwer the 3 problems above, Any simple solution to the Notification System is preferable , thanks....

like image 543
cqcn1991 Avatar asked Mar 13 '13 13:03

cqcn1991


2 Answers

I think you are on the right path.

A slightly better notifications#index

def index
  @notifications = current_user.notications
  @notifications.update_all checked: true
end
  1. Notify this users

    User.uniq.joins(:comments).where(comments: {id: @comment.post.comment_ids}).reject {|user| user == current_user }
    

Unique users that participated in the @comment's post comments, reject (remove from result) current_user.

  1. An observer as pointed out by João Daniel, it is preferred over an after_create. This "Rails best practice" describes it pretty well: http://rails-bestpractices.com/posts/2010/07/24/use-observer
like image 64
Leonel Galán Avatar answered Nov 19 '22 19:11

Leonel Galán


there is an amazing gem called public activity ,,you can customize it as you want and here is a screencast about it in railscast http://railscasts.com/episodes/406-public-activity hope that could help you.

Update

In my rails application I made a similar notifications system like yours to send notifications to all users but in index action you can user

current_user.notifications.update_all(:checked=>true)

and also to send only one notifications to the user once not several times that somebody commented on the post you can use unique_by method

  @comments [email protected]_by {|a| a[:user_id]}

then you can send notifications to only users of previous comments

 @comments.each do |comment|
 comment.user.notifications.create!(....
 end 

hope that could help you

like image 9
Remon Amin Avatar answered Nov 19 '22 18:11

Remon Amin