Comment notification to users in rails?


my webapp has registered users and it has articles, blog posts, gossips too. For all these resources I've a polymorphic Comment model which is listed 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 commentable resource, I've a comment form at the bottom of the commentable resource for users to comment. I want a checkbox, when checked while submitting the comment, the users should receive a notification, be it in Inbox or email since we already have it on user signup, when other new comments are added later.

I want to have some model like Notifications where I could store the commentable_type, commentable_id and user_id (to whom the notification should be sent if there is any new comment created with the matching commentable and user?

How can I implement the association between Comment and Notification? For the checking part if there is any one subscribe to the particular commentable resource is create a CommentObserver with after_create hook to initialize the search and send notifications if there is any matching record.

But I'm confused what the association, model, controller and views would look like to accomplish this? Since the comment model is already polymorphic, can I create the Notification model as polymorphic too??


You can easily accomplish this without a plugin. Create a database table to store User notification subscriptions for posts. Then, each time you create a comment, query the database and send an email using <a href="http://api.rubyonrails.org/classes/ActionMailer/Base.html" rel="nofollow">ActionMailer</a> to all users' address.


First step is 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 add the has_many :notifications to the User, Post, and Comment models.

Once this is done, add the following code to the Comments 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 create a notification once a comment is created. The next step is to edit the Notifications controller so that a notification can be deleted and a 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

Next Thing to do is set up a way to delete the notification when a comment is deleted:

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

Last thing to do is create some views. You can do what ever you want


  • Android GCM push notification without server OR GCM push notification using Microsoft SQL(Not Mysql)
  • Syncronizing database from Active Directory
  • Title cutoff in tabbed page in Xamarin.Forms
  • Trouble downloading file from browser on Android
  • Reload UITableView from another tab
  • how to reopen a class in gems
  • Can my app be notified when another application starts/stops playing audio?
  • Let a function return any type in C++ class
  • Regex for Specific Tag
  • PushKit for VOIP iOS apps
  • Autofac with Web API 2 - Parameter-less constructor error
  • Android changing fragment order inside FragmentPagerAdapter
  • Why cepheus don't send int without quotes to orion?
  • how does System.Web.HttpRequest::PathInfo work?
  • Adding elements to a huge XML file
  • Diff between two dataframes in pandas
  • Not able to aggregate on nested fields in elasticsearch
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Django simple Captcha “No module named fields” error
  • Do I need to reset a Perl hash index?
  • How to define and use opencv mat of user type
  • Swift: Switch statement fallthrough behavior
  • Use of this Javascript
  • MongoDB in PHP using aggregate to group by _id is null not working
  • How do I change content of ComboFieldEditor?
  • C# - Serializing and deserializing static member
  • Bug in WPF DataGrid
  • Incrementing object id automatically JS constructor (static method and variable)
  • Cassandra Data Model
  • Weird JavaScript statement, what does it mean?
  • Do I've to free mysql result after storing it?
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • SQL merge duplicate rows and join values that are different
  • Free memory of cv::Mat loaded using FileStorage API
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Getting Messege Twice Using IMvxMessenger
  • How does Linux kernel interrupt the application?
  • unknown Exception android
  • Python/Django TangoWithDjango Models and Databases
  • git trying to push non-existent file … after clearing cache