Questions tagged [counter-cache]

129 questions
5
votes
3 answers

How to implement a Counter Cache in Rails?

I have a posts controller and a comments controller. Post has many comments, and comments belong to Post. The associate is set up with the counter_cache option turned on as such: #Inside post.rb has_many :comments #Inside comment.rb belongs_to…
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
4
votes
2 answers

Rails 3 and Rspec: counter cache column being updated to 2 when expected 1

I'm testing with Rspec a model named Solutions which has many Likes. Solution stores how many Likes it have (counter_cache). It has a "likes_count" attribute (and respective db field). When I create a Like record associated to a Solution, I expect…
Bruno Dias
  • 216
  • 1
  • 7
4
votes
2 answers

counter_cache not decrementing for has_many associations in ActiveReord

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…
Joost Schuur
  • 4,417
  • 2
  • 24
  • 39
4
votes
1 answer

Rails 4: counter_cache in has_many :through association with dependent: :destroy

Although similar questions have already been asked: counter_cache with has_many :through dependent => destroy on a "has_many through" association has_many :through with counter_cache none of them actually addresses my issue. I have three models,…
4
votes
1 answer

Write fixtures with counter_cache columns

Write fixtures that have counter_cache columns is tedious and complicated, I tried use method reset_counters in test_helper.rb but it didn't work. Same way to put it working ?
Lázaro Nixon
  • 61
  • 1
  • 3
4
votes
1 answer

Counter cache for scopes

I've set up some counter_caches in my app before but only for simple belongs_to relationships. I'm doing a lot of queries like user.collections.got.count where got is a scope in my collection model belongs_to :user scope :got, -> { where(status:…
Ossie
  • 1,103
  • 2
  • 13
  • 31
4
votes
2 answers

Rails ActiveRecord CounterCache and Callbacks

Does counter_cache increments and decrements fire active_record callbacks ? User << AR has_many :cups after_update :do_something def do_something "Will I be called when number of cups updated ?" end end Cup << AR belongs_to…
Abid
  • 7,149
  • 9
  • 44
  • 51
4
votes
2 answers

Adding counter cache to self join table in rails

I am trying to add a counter cache on a a column in a self join association. I have two models User and followings. User has followers and followees, who are from the user table itself. User.rb has_many :followings has_many :followers, :through…
abhilash
  • 1,587
  • 2
  • 10
  • 17
3
votes
1 answer

counter cache doesn't update but I can save to the parent and the child

I added a counter cache but can't get it to update. But I can update the parent - the Blog Post model by adding a new blog post - and I can update the child - Comments model - by adding a new comment. The counter cache is supposed to keep track of…
pez_dispenser
  • 4,394
  • 7
  • 37
  • 47
3
votes
1 answer

Issue with running a migration on heroku

This is a follow up to this post Rails, data structure and performance, where I have attempted to create a Counter Cache on Rails. To not have it to default 0, I have also added updating the column to the existing count value in the…
Emmanuel
  • 254
  • 3
  • 11
3
votes
5 answers

How can I invoke the after_save callback when using 'counter_cache'?

I have a model that has counter_cache enabled for an association: class Post belongs_to :author, :counter_cache => true end class Author has_many :posts end I am also using a cache fragment for each 'author' and I want to expire that cache…
Carter
  • 88
  • 7
3
votes
0 answers

Using counter_culture, can't get conditional counter cache working

I cannot seem to get counter_culture to work with a conditional counter cache. It is working perfectly with the normal counter cache. Certificate approving and disapproving increments and decrements as expected. So in my db, for Packet, slip_count…
Tache
  • 495
  • 3
  • 9
3
votes
1 answer

Rails 4.1 counter cache not updating

This counter cache does not seem to be working correctly, although to my knowledge I have done everything correctly. user.rb: class User < ActiveRecord::Base has_many :search_stats, counter_cache: true end migration: class…
a-b-r-o-w-n
  • 515
  • 2
  • 17
3
votes
3 answers

Update a counter cache column after restoring a record with paranoia (acts_as_paranoid)

I have a counter_cache column in my model. I use acts_as_paranoid for this model (Paranoia gem). How to update a counter cache column for an associated record when I restore a record?
3
votes
2 answers

Accessing ruby counter cache

I'm playing around with a fork of acts_as_taggable_on_steroids as a learning exercise. The version I'm looking at does some stuff I don't understand to calculate Tag counts. So I thought I'd do a version using PORC (Plain Old Rails Counters): class…
Julian H
  • 1,809
  • 17
  • 18
1
2
3
8 9