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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:45:28+00:00 2026-05-18T08:45:28+00:00

My Rails 3 app has 2 models and a third that’s join table between

  • 0

My Rails 3 app has 2 models and a third that’s join table between them and their has_many relationships. Basically, User and Show are joined by SavedShow, allowing users to save a list of shows:

class Show < ActiveRecord::Base
  has_many :saved_shows
  has_many :users, :through => :saved_shows
end

class User < ActiveRecord::Base
  has_many :saved_shows
  has_many :shows, :through => :saved_shows
end

class SavedShow < ActiveRecord::Base
  belongs_to :user, :counter_cache => :saved_shows_count
  belongs_to :show
end

I’ve noticed that the counter_cache field (shows_saved_count) gets incremented automatically just fine, but not decremented. The core of the issue seems to be that removing shows from a user’s list is done via delete, which does not trigger updating of the counter_cache:

current_user.shows.delete(@show)

However, I can’t call the destroy method here, since that not only deleted the User/Show association in SavedShow, but also the Show object itself, which is not what I want.

Is a counter_cache in this kind of scenario not an appropriate use?

There appears to be a discussion about this as a bug back in 2009, and fixes were discussed, but I’m still seeing the issue in the latest Rails 3.0.

I would just write my own custom handling in the model, but there seems to be no after_delete callback that I can hook into (presumably this is the reason decrementing doesn’t work in the first place). Right now, there’s only one place in my own code where a delete of the association could occur, so I’ll just manually make a call to update the counter, but this seems like like such a fundamental shortcoming or bug of ActiceRecord associations with counter_cache, that I’m wondering if I’m not just missing something.

If this is indeed a genuine problem with counter_caches, what would be the best workaround?

  • 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-18T08:45:29+00:00Added an answer on May 18, 2026 at 8:45 am

    Same issues here but on Rails 2.3.
    Worth noticing that also adding a touch, like:

    belongs_to :user, :counter_cache => :saved_shows_count, :touch => true
    

    Won’t update counter cache nor the related updated_at field on association.delete(object).

    To workaround the issue usually we manipulate the join model, but that also have some drawbacks.

    Patch is here: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2824-patch-has_many-through-doesnt-update-counter_cache-on-join-model-correctly#ticket-2824-18

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

Sidebar

Related Questions

I have a rails app that has picked up a bit of traction, but
I'm working on a rails app that has a whole bunch of before filters
I'm building a Rails app that has Etsy.com style functionality. In other words, it's
I've created an app that has several models (say A, B) that are polymorphically
I'm working on a rails app that has an IPv6 model. I'm storing the
My Rails-app has a sign in box with a remember me checkbox. Users who
I need to store app specific configuration in rails. But it has to be:
I have a rails app that I have serving up XML on an infrequent
I have a Rails app that I have successfully tested with Mongrel and Webkit.
I have a Rails app that I need to deploy. Here are the facts:

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.