C
C
Cyril2020-06-27 20:23:04
Database design
Cyril, 2020-06-27 20:23:04

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

1 answer(s)
R
Roman Mirilaczvili, 2020-06-27
@2ord

Comment

  • id
  • author_id
  • body
  • commentable_type (Article, Post, ...)
  • commentable_id
  • is_hidden (hide if deleted by moderator or author)

t.references :commentable, polymorphic: true
Like
  • id
  • author_id
  • likeable_type (Article, Post, Comment, ...)
  • likeable_id
  • points (+1,-1)

You don't need to delete likes. Collect all +1 and -1 together and then calculate their sum. The counting of likes does not have to be instant and 100% accurate.
When the after_update callback fires, do not update immediately, but send an event to the queue. The worker will pick up the event and process it. Check the time of the last update and if it exceeded, say, 1 minute (so as not to load the DBMS), update the account.
Notifications table - by analogy with comments.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question