Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Custom counter caches not being used

#1683

If a belongs to uses a custom counter cache that is not of the form #{attribute_name}_count of the corresponding has_many association then they are not used.

A simple solution is to add a :counter_cache option to the has_many association.

The attached patch is complete with docs and tests.

Reported by Adam · January 2nd, 2009 @ 12:18 AM

State: new
Milestone: 3.x
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Pratik
    • Tag set to activerecord, patch
    • State changed from new to incomplete

    I think the key needs to be named differently, probably :counter_cache_attribute or something. Also, the tests shouldn't really use mocking/stubbing.

    Thanks.

    March 7th, 2009 @ 07:15 PM

  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord, patch to activerecord, bugmash, patch

    August 5th, 2009 @ 03:13 PM

  3. Derander
    Derander

    -1, this is fixed in 2-3-stable and master.

    August 9th, 2009 @ 09:49 PM

  4. Derander
    Derander

    To verify the fix,

    rails test2

    cd test2

    script/generate model Post title:string custom_count:integer

    script/generate model Comment title:string post_id:integer

    In post.rb: has_many :comments

    in comment.rb: belongs_to :post, :counter_cache => :custom_count

    ./script/console

    p = Post.create(:title => "A Post")

    p.comments.build(:title => "A comment")

    p.save

    p.custom_count should be 1

    August 9th, 2009 @ 09:52 PM

  5. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord, bugmash, patch to activerecord, patch
    • State changed from incomplete to resolved

    August 10th, 2009 @ 02:38 AM

  6. Derander
    Derander
    • Tag changed from activerecord, patch to activerecord, patch

    Jose Valim found an error in my understanding of the patch.

    The problem is not found by examining the value of p.custom_count, instead, starting from p.save:

    p.custom_count = 10

    p.save

    p.comments.count will be 1. #It should be 10 if custom_count were being used.

    August 10th, 2009 @ 08:47 AM

  7. José Valim
    José Valim
    • State changed from resolved to new

    Since we added :inverse_of in master (Rails 3.0), we could make use of it to retrieve the counter cache column, instead of adding a new option to has_many which may be confusing.

    August 10th, 2009 @ 08:51 AM

  8. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  9. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 21st, 2010 @ 03:36 AM

  10. Jeff Kreeftmeijer
  11. Jeff Kreeftmeijer
  12. Aditya Sanghi
    Aditya Sanghi
    • Title changed from Add magic encoding comment to generated files to Custom counter caches not being used

    October 24th, 2010 @ 02:40 PM

  13. bingbing