Notify user comments in rails?

my webapp has registered users and it has articles, blog posts, gossip. For all these resources, I have a polymorphic comment model, which is given below.

id content commentable_id commentable_type user_id created_at updated_at 1 Frist comment 2 Article 1 .... 2 Second comment 3 Post 2 ..... 

So, for each remarkable resource, I have a comment form at the bottom of the comment for user comments. I want the flag to be checked when sending a comment, users should receive a notification, whether in the inbox or in the email, since we already have this on user registration when other new comments are added later.

I want to have some model, such as Notifications, in which I could store commentable_type, commentable_id and user_id (who needs to be notified if there is a new comment created with the corresponding comment and user?

How can I implement the relationship between a comment and a notification? For the verifying part, if there is any subscription to a specific comment, create a CommentObserver with an after_create hook to initiate the search and send notifications if there is a corresponding entry.

But am I confused about what the association, model, controller, and views will look like? Since the comment model is already polymorphic, can I create a notification model as polymorphic too?

+1
source share
2 answers

You can easily accomplish this without a plugin. Create a database table to store a subscription to user notifications for messages. Then each time you create a comment, query the database and send an email using ActionMailer to all user addresses.

+6
source

The first step is to create a new model and controller for notification

  $ rails g model Notification post:references comment:references user:references read:boolean $ rake db:migrate $ rails g controller Notifications index 

Once this is done, the next step is to add has_many: notifications in the User, Post, and Comment models.

Once this is done, add the following code to the comment model:

  after_create :create_notification private def create_notification @post = Post.find_by(self.post_id) @user = User.find_by(@post.user_id).id Notification.create( post_id: self.post_id, user_id: @user, comment_id: self, read: false ) end 

The above snippet creates a notification after creating a comment. The next step is to edit the notification controller so that the notification can be deleted and the user can mark the notification as read:

  def index @notifications = current_user.notications @notifications.each do |notification| notification.update_attribute(:checked, true) end end def destroy @notification = Notification.find(params[:id]) @notification.destroy redirect_to :back end 

The next thing to do is a way to delete a notification when deleting a comment:

  def destroy @comment = Comment.find(params[:id]) @notification = Notification.where(:comment_id => @comment.id) if @notification.nil? @notification.destroy end @comment.destroy redirect_to :back end 

The last thing to do is to create several views. You can do whatever you want

+1
source

Source: https://habr.com/ru/post/1232048/


All Articles