Question

Within a simple CRUD rails app i'm working on, i'm using a comments gem called Opinio

The model that is commentable is called posts. Within my view I am trying to output the comments count per post:

 <%= post.comments.size %>

This is only yielding the number of parent comments. It is not including the replies. How do I get the total number of comments per post, including the replies? Where is this gem storing the comment replies?

Any help would be much appreciated!

Was it helpful?

Solution

I've browsed around the source code and apparently comments may be children of other comments but only up to one level: e.g. comment1 belongs to post1 and comment2 belongs to comment1 but comment3 can not belong to comment2 (because the parent of comment2 is already another Comment).

So the query you need to make sounds like this: fetch all parent comments of post1, then fetch all child comments of the previous parent comments, then merge results. Something like:

class Post

  # This returns an Array of Comment objects. If you intend to use 
  # more than the #count method on the result, you should consider 
  # adding some sort of ordering on the two queries.
  def all_comments
    parent_comments = Comment.where(:commentable_id => id)
    child_comments = Comment.where(:commentable_id => parent_comments.map(&:id))
    parent_comments + child_comments
  end

end

post = Post.find(1)
post.all_comments.count # The number of all (parent & child) comments of post

If your comment class name is not Comment feel free to change it to whatever (same for Post).

OTHER TIPS

You could try to retrieve the total number of comments on an individual post by using:

@post = Post.find(params[:id])in the controller and

<%= post.comments.count %>in the view.

To get all comments posted you could try

@posts = Post.all in the controller and

<%= @posts.commments.count %> in the view.

The comment replies will be stored in the database. Check the \db\migrate\ folder in your app and you should see the comments migration. If you look in the \db\ folder you should notice a development.sqlite3 database. Opening this using a database viewer will allow you to see the stored data.

Update 1

After reading through the Github repo I spotted a couple of things. Firstly I assume you are using Rails 3? (otherwise the engine doesn't work)

To display comments for a specific item opinio provides a method:

opinio_identifier do |params| next Review.find(params[:review_id]) if params[:review_id] next Product.find(params[:product_id]) if params[:product_id] end

On this method you receive the params variable and you tell the engine who owns the comments from that page. This allows you to use routes like:

/products/1/comments

/products/1/reviews/1/

Ttwo customizations are only made through the opinio initializer, and they are the accept_replies which defaults to true and strip_html_tags_on_save which also defaults to true. You should be able to find this in the commentable model.

Maybe you could try this:

<%= Opinio.model_name.comments.count %> would change to

<%= Opinio.post.comments.count %>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top