Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8449237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:38:49+00:00 2026-06-10T10:38:49+00:00

I am trying to show into article#index how many comments where on each articles.

  • 0

I am trying to show into article#index how many comments where on each articles.

So i have the following models

resources Article do
 resources Comments do
 end
end

I know in each article I can do the follow and this would work:

@count = @article.comments.find(:all).count

And just show in the view count. But then problem comes when i am in an index file and not sure how to show how many comments exist for this event atm.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:38:50+00:00Added an answer on June 10, 2026 at 10:38 am

    articles_controller.rb

    def index
      @articles = Article.all
    end
    

    articles/index.html.erb

    <% @articles.each do |article| %>
      comments in article <%= article.comments.count %>
    <% end %>
    

    The nested routes (comments within articles) matters more in terms of your create/destroy routes for comments. Also be sure to add accepts_nested_attributes_for :comments in your Article model. This will allow you to do stuff like this:

    for example, in articles_controller.rb

    def show
      @article = Article.find(params[:id])
    
      # creates a new comment object with atricle_id already initialized
      @comment = @article.comments.build
    end
    

    Edit

    If you do start caring about performance, I agree with Kitto’s comment.

    Add this migration:

    class AddCommentsCountToArtices < ActiveRecord::Migration
      def change
        add_column :articles, :comments_count, :integer, null: false, default: 0
      end
    end
    

    And change your relation declaration in your Comment model to this:

    belongs_to :article, counter_cache: true
    

    Then you can make calls like this article.comments_count to get the count instead of atricle.comments.count. And it’s great if the count is 0, because it doesn’t even make the query (p. 195 of The Rails 3 Way).

    If you’re curious about how counter_cache works: it adds a callback to the belonging class (in this case, the Comment class) that updates the comments_counter attribute on the parent article each time a comment is created or destroyed.

    Also, counter_cache functionality can easily be added to existing databases as demonstrated here, by Obie Fernandez.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

inside show.js.erb $('#viewport').html(<%= escape_javascript(render('show')) %>); I am trying to render the show view into
I'm trying to get the output of the query 'SHOW TABLES FROM database_name' into
I'm trying to show a list of links (in index.html) to text files in
I have the article class with several private vars. Then I'm trying to open
Actually i'm trying to show and dialog into a parent form, An reference example
I am trying to show xml data in an Ultragrid. I have tried using
I'm trying show on views (each row) my custom form. It works, but the
I'm trying to show a Chart from google visualization api into a jQuery dialog
I'm trying to show TXT file (ASCII) into UIVewView . For example, using the
Some Background I have an iPhone app with three UIWebViews, each used to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.