Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you create a notification system like on SO or Facebook in RoR?

I'm thinking that notifications would be it's own resource and have a has_many, through relationship with the user model with a join table representing the associations.

A user having many notifications is obvious, and then a notification would have many users because there would be a number of standardized notifications (a commenting notification, a following notification etc.) that would be associated with many users.

Beyond this setup, I'm unsure how to trigger the creation of notifications based on certain events in your application. I'm also a little unsure of how I'd need to set up routing - would it be it's own separate resource or nested in the user resource? I'd find it very helpful if someone could expand on this.

Lastly, ajax polling would likely improve such a feature.

There's probably some things I'm missing, so please fill this out so that it is a good general resource.

like image 307
Justin Meltzer Avatar asked Apr 11 '11 04:04

Justin Meltzer


2 Answers

So the general gist:

1) Notifications would be a polymorphic association in that comments can have many notifications, users can have many notifications, a 'following' can have many notifications etc.

2) You can have Model Observers, where you can "observe" certain events, such as when a new comment is created. This is would be your triggers.

In terms of routing, you really don't need to do anything out of the norm. The only routing you may have is a domain.com/notifications where it shows all the notifications.

Notification table might look like:

sender_id: integer, receiver_id: integer, notifiable_id: integer, notifiable_type: string

like image 176
Mike Lewis Avatar answered Oct 15 '22 22:10

Mike Lewis


  1. For a notification system I personally prefer server push technology. Ryan Bates (the voice behind Railscasts) has a great screen cast that you might want to watch

  2. For triggering actions for particular event, have a look at 'Observers' as @mike mentioned

like image 40
sameera207 Avatar answered Oct 15 '22 22:10

sameera207