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??Answer1:
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.Answer2:
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