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 7622845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:33:23+00:00 2026-05-31T04:33:23+00:00

I need to update attribute :average_rate when new comment is added. I have in

  • 0

I need to update attribute :average_rate when new comment is added.
I have in comments.rb

  belongs_to :page, :counter_cache => true

and in page.rb

  has_many :comments
  after_save :update_average_rate

and update_average_rate method in page.rb

  def update_average_rate(comment)
    if comments_count_changed?
      write_attribute :average_rate, (comments.sum(:rate) / comments.count.to_f).ceil
    end
  end

but it doesnt work. When I am doing

raise comments_count_changed?.inspect

in update_average_rate method, it outputs “false” , but comments_count is changed. What I am doing wrong? 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-05-31T04:33:24+00:00Added an answer on May 31, 2026 at 4:33 am

    Your problem is that counter updates don’t actually set the “changed” flags to true.

    For a column/attribute a, a_changed? will be true if and only if a has been changed but not saved to the database. The basic behavior goes like this:

    1. Load or create o. o.a_changed? will be false.
    2. o.a = pancakes, o.a_changed? will be true.
    3. o.save, o.a_changed? will be false.

    You’re using :counter_cache but internally, that uses update_counters and that:

    simply does a direct SQL update for the record with the given ID, altering the given hash of counters by the amount given by the corresponding value

    So after update_counters has been called, the counter attribute will not be marked as changed as the counter value in the database will be the new one.

    I think you’ll have to move your average_rate logic into an after_save callback on Comment.

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

Sidebar

Related Questions

I need to update a combobox with a new value so it changes the
I need to update the comments field in a table for a large list
I need to update an existing SharePoint content type by adding a new field
I have an old product that I need to update, written in C# and
Update from comment: I need to extend linq-to-sql classes by own parameters and dont
I have a problem trying to update an attribute of one model using the
I need to update select occurrences in different tables of the [ISD_ID] attribute, how
I need to update a portlet on the WebSphere Portal 6.0. I have tried
I need to update my Facebook Fan Page in a django app so I
I need to update a record in a database with the following fields [ID]

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.