How would you create a notification system like on SO or Facebook in RoR?
Solution 1
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
Solution 2
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
For triggering actions for particular event, have a look at 'Observers' as @mike mentioned
Related videos on Youtube
Justin Meltzer
Updated on March 20, 2020Comments
-
Justin Meltzer about 4 years
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.
-
Justin Meltzer about 13 yearswhat exactly would sender and receiver be? they would be resources of their own?
-
Mike Lewis about 13 yearssender and receiver would both be foreign keys for users.
-
Justin Meltzer about 13 yearsgot it, are there any more in depth resources for learning the ins and outs of observers?
-
Mike Lewis about 13 yearsTruth be told, there really isn't much to it. That page I linked to and this: api.rubyonrails.org/classes/ActiveRecord/Observer.html is really all you need.
-
Justin Meltzer about 13 yearsyeah... so it's essentially just callbacks outside of the model
-
Mike Lewis about 13 years@Justin, exactly. It's awesome how easy it is, yet very powerful.
-
Nirav Shah almost 13 yearsI came across this link: mickeyben.com/2010/05/23/…
-
Shreyas over 12 years@Mike How exactly would you set the user id ? This has been a source of confusion for me and I posted a question on SO regarding this - stackoverflow.com/questions/7481527/…
-
FireDragon over 10 yearsjust wanted to point out that Observers are depricated and no longer available in Rails 4. I'm still on Rails 3 but don't want to use Observers so it's easier to upgrade. Here's another post with alternatives stackoverflow.com/questions/15165260/…