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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:08:55+00:00 2026-06-02T07:08:55+00:00

Suppose I have a Rails app that deals with Posts and Comments . A

  • 0

Suppose I have a Rails app that deals with Posts and Comments. A Post has_many Comments and each Comment belongs_to a Post.

Each Comment has a word_count property. The Post object has an average_comment_word_count property which is an average of each of the Comment’s word_count.

When I send a DELETE request on a particular comment, the app should recalculate the average_comment_word_count parameter. The way I thought it would work is like this:

class Comment < ActiveRecord::Base

belongs_to :user
belongs_to :post
after_destroy :update_post_average_word_count

def update_post_average_word_count
  post_average_word_count = 0
  post.comments.each do |comment|
     post_average_word_count = post_average_word_count + comment.word_count
  end

  post_average_word_count / post.comments.count
  post.update_attributes average_word_count: post_average_word_count
end

However I get a database error because the app cannot find a reference to the post in question (because the comment is gone, cue Homer Simpson here)

Is there a different method for accomplishing this?

  • 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-02T07:08:55+00:00Added an answer on June 2, 2026 at 7:08 am

    Use before_destroy and take this into account when calculating the post_average_word_count:

    post.update_attributes average_word_count: (post_average_word_count - self.word_count)

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

Sidebar

Related Questions

Suppose I have a client side app that sends out requests to a rails
Suppose I have the following view in my rails app <% if is_subscriber? %>
Suppose I have three tables in my Rails app: cats , dogs , and
I have a Rails app that processes a large (millions) number of records in
I have a standard Rails app that uses a database, sqlite3 and the webrick
Have a rails app that is supposed to display a list of products/managers. After
I have a partial in my rails app that loads the vote form (It's
I have a Rails app that uses a lot of custom statistics code I
Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications -
In Rails 3.0.9 (Ruby 1.9.2, MySQL) I have a method that is supposed to

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.