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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:02:30+00:00 2026-05-30T06:02:30+00:00

Using Rails 3.1.3 and I’m trying to figure out why our counter caches aren’t

  • 0

Using Rails 3.1.3 and I’m trying to figure out why our counter caches aren’t being updated correctly when changing the parent record id via update_attributes.

class ExhibitorRegistration < ActiveRecord::Base
  belongs_to :event, :counter_cache => true
end

class Event < ActiveRecord::Base
  has_many :exhibitor_registrations, :dependent => :destroy
end

describe ExhibitorRegistration do
  it 'correctly maintains the counter cache on events' do
    event = Factory(:event)
    other_event = Factory(:event)
    registration = Factory(:exhibitor_registration, :event => event)

    event.reload
    event.exhibitor_registrations_count.should == 1

    registration.update_attributes(:event_id => other_event.id)

    event.reload
    event.exhibitor_registrations_count.should == 0

    other_event.reload
    other_event.exhibitor_registrations_count.should == 1
  end
end

This spec fails indicating that the counter cache on event is not being decremented.

1) ExhibitorRegistration correctly maintains the counter cache on events
   Failure/Error: event.exhibitor_registrations_count.should == 0
     expected: 0
          got: 1 (using ==)

Should I even expect this to work or do I need to manually track the changes and update the counter myself?

  • 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-30T06:02:31+00:00Added an answer on May 30, 2026 at 6:02 am

    From the fine manual:

    :counter_cache

    Caches the number of belonging objects on the associate class through the use of increment_counter and decrement_counter. The counter cache is incremented when an object of this class is created and decremented when it’s destroyed.

    There’s no mention of updating the cache when an object is moved from one owner to another. Of course, the Rails documentation is often incomplete so we’ll have to look at the source for confirmation. When you say :counter_cache => true, you trigger a call to the private add_counter_cache_callbacks method and add_counter_cache_callbacks does this:

    1. Adds an after_create callback which calls increment_counter.
    2. Adds an before_destroy callback which calls decrement_counter.
    3. Calls attr_readonly to make the counter column readonly.

    I don’t think you’re expecting too much, you’re just expecting ActiveRecord to be more complete than it is.

    All is not lost though, you can fill in the missing pieces yourself without too much effort. If you want to allow reparenting and have your counters updated, you can add a before_save callback to your ExhibitorRegistration that adjusts the counters itself, something like this (untested demo code):

    class ExhibitorRegistration < ActiveRecord::Base
      belongs_to :event, :counter_cache => true
      before_save :fix_counter_cache, :if => ->(er) { !er.new_record? && er.event_id_changed? }
    
    private
    
      def fix_counter_cache
        Event.decrement_counter(:exhibitor_registration_count, self.event_id_was)
        Event.increment_counter(:exhibitor_registration_count, self.event_id)
      end
    
    end
    

    If you were adventurous, you could patch something like that into ActiveRecord::Associations::Builder#add_counter_cache_callbacks and submit a patch. The behavior you’re expecting is reasonable and I think it would make sense for ActiveRecord to support it.

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

Sidebar

Related Questions

Using Rails 3. In my project directory am trying to launch rails script/console by
Using RAILS 2.2 Hello I am having an issue where my dates are being
Using Rails I'm trying to get an error message like The song field can't
We are using Rails 3.0.X and running our web app on Heroku. We are
Using: Rails 3.1.1 I am trying to create a search engine in my application
We're building an app, our first using Rails 3, and we're having to build
Using rails, devise, rspec & factorygirl: Trying to create some tests for my site.
using rails 3.2.8, I have an application where users can sign in and out,
Using Rails 3.2.8, Devise 2.1.2, & Simpleform 2.0.2 I'm trying to redirect user upon
Using Rails 3.0.9, I'm trying to redirect after a post to the create method

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.