How do I count unread comments using “Unread” Ruby gem?


How do I count unread comments using “Unread” Ruby gem?



comments_helper.rb:


module CommentsHelper
def unread_comments_count
@comments_count = Comment.unread_by(user).count
end
end



In my ApplicationController, I defined user:


ApplicationController


user


def user
@user = User.find_by(params[:user_id])
end



I could have used current_user, but the result I got (with that) is total number of comments in my database. So, I thought I should use user. But it didn't work.


current_user


user



view:


<%= unread_comments_count %>



However, the result is still total number of all the comments made on all the posts by all users. I can't actually figure out what I am doing wrong because I have carefully done everything right in my Models as guided by the gem.



I would appreciate any clue to fix this.



Update



The query it generates is:


SELECT COUNT(*) FROM "comments" 'LEFT JOIN read_marks ON read_marks.readable_type = "Message" AND read_marks.readable_id = comments.id AND read_marks.reader_id = 1 AND read_marks.reader_type = 'User' AND read_marks.timestamp >= comments.created_at WHERE read_marks.id IS NULL





Try running the same thing Comment.unread_by(user).count in rails console. See if it gives the expected output.
– Jagdeep Singh
Jul 2 at 6:50


Comment.unread_by(user).count


rails console





It doesnt return anything... I am kinda thinking there is something i havent added to my database that wasnt added during unread gem generated some migration files...
– Sunny
Jul 3 at 11:15





Could there be any gem you have used in the past or any working online source i can use to just track total number of unread comments made on a post belonging to a current_user?
– Sunny
Jul 3 at 11:16





"It doesnt return anything." - you can check the database query it fires.
– Jagdeep Singh
Jul 3 at 11:18





FROM Comments 'LEFT JOIN read_marks ON read_marks.readable_type = "Message" AND read_marks.readable_id = comments.id AND read_marks.reader_id = 1 AND read_marks.reader_type = 'User' AND read_marks.timestamp >= messages.created_at WHERE read_marks.id IS NULL
– Sunny
Jul 3 at 11:25










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?