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

  • Home
  • SEARCH
  • 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 8665219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:25:44+00:00 2026-06-12T17:25:44+00:00

I am using Ruby on Rails 3.2.2 and I would like to set a

  • 0

I am using Ruby on Rails 3.2.2 and I would like to set a counter cache value to a “custom” one. That is, at this time (in my migration file) I am trying to use the following code:

def up
  add_column :articles, :comments_count, :integer, :default => 0

  Article.reset_column_information

  Article.find_each do |article|
    # Note: The following code doesn't work (when I migrate the database it   
    # raises the error "comments_count is marked as readonly").
    Article.update_column(:comments_count, article.custom_comments.count)
  end
end

In other words, I would like to set the :comments_count value (a counter cache database table column) to a custom value (in my case that value is article.custom_comments.count – note: the custom_comments is not an ActiveRecord Association but a method stated in the Article model class; it returns an integer value as well) that is not related to a has_many associations.

Maybe, I could / should use something like

Article.reset_column_information

Article.find_each do |article|
  Article.reset_counters(article.id, ...)
end

but it seems that the reset_counters method cannot work without has_many associations.

How can I set the :comments_count counter cache value to a given value that is related to a “custom association”?

  • 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-12T17:25:45+00:00Added an answer on June 12, 2026 at 5:25 pm

    You describe comments_count as a counter cache, yet a counter cache is strictly defined as the number of associated records in a has_many relationship, which you say this isn’t.

    If the only way to get the value you want is via method on Article, then you’re going to have to iterate over all your Article objects and update each one.

    Article.find_each do |article|
      article.update_attribute(:comments_count, article.custom_comments.count)
    end
    

    This is pretty inefficient, since it’s loading and saving every object.
    If the definition of custom_comments (which you don’t actually explain) is something you can express in SQL, it would undoubtedly be faster to do this update in the database. Which might look something like this:

    CREATE TEMP TABLE custom_comment_counts_temp AS
      SELECT articles.id as id, count(comments.id) as custom_comments 
        FROM articles 
        LEFT JOIN comments ON articles.id = comments.article_id
        WHERE <whatever condition indicates custom comments>
        GROUP BY articles.id;
    
    CREATE INDEX ON custom_comments_counts_temp(id);
    
    UPDATE articles SET comments_count = (SELECT custom_comments FROM custom_comment_counts_temp WHERE custom_comment_counts_temp.id = articles.id);
    
    DROP TABLE custom_comment_counts_temp;
    

    (this assumes postgresql – if you’re using mySQL or some other database, it may look different. If you’re not using a relational database at all, it may not be possible)

    Additionally, since it’s not a counter cache according to Rails’ fairly narrow definition, you’ll need to write some callbacks that keep these values updated – probably an after_save callback on comment, something like this:

    comment.rb:

    after_save :set_article_custom_comments
    
    
    def set_article_custom_comments
      a = self.article
      a.update_attribute(:comments_count, a.custom_comments.count)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3.0.7 and I would like to set a
I am using Ruby on Rails 3 and I would like to set header
I am using Ruby on Rails 3 and I would like to set a
I am using Ruby on Rails 3 and I would like to set some
I am using Ruby on Rails 3.2.2 and I would like to know if
I am using Ruby on Rails 3.2.2 and I would like to know what
I am using Ruby on Rails 3.2.2 and I would like to know if
I am using Ruby on Rails 3.2.2 and in my controllers I would like
I am using Ruby on Rails 3.2.2. and I would like to know if
I am using Ruby on Rails 3.2.2 and rspec-rails-2.8.1. I would like to output

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.