Answer the question
In order to leave comments, you need to log in
How to properly implement a notification system in RoR?
There is some system on RoR, in which there is a mechanism for commenting on an article, liking an article, assigning a status to an article, and sending an article to the "Honor Board". The main object of attention is the Article.
The article has its own model - Article. And accordingly (has_many) models that are associated with the Article are likes (Like), comments (Comment).
Currently, all events are registered using callbacks in Article. That is, if an article is liked, a comment is left, the status is changed, or the article is sent to the leaderboard, then the after_update callback fires, which calls some createNotification method located in the helper class.
The createNotification method, in turn, adds all notifications to the general notifications table.
How, in your opinion, would it be correct to store notifications in notifications if, in addition to adding an event, you also need to rollback the event? Let's say the Like is put, the event is added. And if this like is removed, then the record about this like is removed from the table.
It's easy with Likes. Because only the one who puts it can remove the like. That is, the corresponding entry in the notifications table is easy to find.
But what about comments, if a comment can be deleted not only by the creator of the comment, but also by users with a role, for example, Moderator/Admin/etc. ? How can I remove it from notifications if I store only the article ID in the notifications table? It turns out that you need to save the comment ID too? After all, the relationship between the Comment and Article models is one-to-many.
At the moment, in the notifications table, I store the type of notification (string - like, comment, status or honors board), the ID of the entity being changed (in this case, the article ID), the ID of the user who performed the action (like, comment, etc.) .
I want the storage in notifications to be universal. So that not only can you save notifications related to the Article, but also to anything else.
Answer the question
In order to leave comments, you need to log in
Comment
t.references :commentable, polymorphic: true
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question