Questions tagged [counter-cache]
129 questions
3
votes
1 answer
counter_cache is stale in after_create hook
I rely on a counter cache value in an after_create hook of my model. However, my hook is called before the counter cache gets updated, thus breaking a computation.
Is there any way to force a counter cache "flush" so that I always see an up-to-date…

mxk
- 43,056
- 28
- 105
- 132
3
votes
3 answers
Mongoid and counter_cache column
I have Comment model:
class Comment
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :commentable, polymorphic: true, touch: true#, counter_cache: true
When I run:
Article.first.comments.count => 1 #without counter_cache:…

FastIndian
- 222
- 4
- 12
3
votes
1 answer
Why is counter_cache column not increasing when << an association?
Given I have the following models:
class Location < Active::Record
has_many :storables, foreign_key: :bin_id
# ...
end
class Storable < Active::Record
belongs_to :bin, class_name: :Location, counter_cache: true
# ...
end
When I run the…

Dane O'Connor
- 75,180
- 37
- 119
- 173
3
votes
3 answers
Rails has_many through polymorphic counter cache
I have two models I link together using a polymorphic has_many through association and I would like to add a counter_cache but it seems Rails/ActiveRecord does not support this feature out of the box.
class Classifiable < ActiveRecord::Base
…

Jonathan Roy
- 903
- 11
- 20
3
votes
2 answers
Rails Models counter_cache column initialization
I'm using rspec for testing and hornsby scenarios for object graphs used in tests.
Is it good practice to initialize counter cache columns to 0 value instead of leaving them uninitialized (nil)? Or should i define default value in migrations that…

Eimantas
- 48,927
- 17
- 132
- 168
3
votes
3 answers
Why do I get this error if I try to make counter cache?
I'm using Rails 3.2.6. When I try to make counter cache, I get this error somehow.
How can I fix this? I've done the same thing on this app but not on this model.
What's wrong with my code or association?
command bundle exec rake db:migrate
Log
== …

Foo
- 800
- 1
- 7
- 17
3
votes
2 answers
counter_cache not updating on the model after save
I am using a counter_cache to let MySQL do some of the bookkeeping for me:
class Container
has_many :items
end
class Item
belongs_to :container, :counter_cache => true
end
Now, if I do this:
container = Container.find(57)
item =…

sehnsucht
- 55
- 5
3
votes
2 answers
How to set a counter cache value to a given one?
I am using Ruby on Rails 3.2.2 and I would like to set a counter cache value to a "custom" one. That is, at this time (in my migration file) I am trying to use the following code:
def up
add_column :articles, :comments_count, :integer, :default =>…

user12882
- 4,702
- 9
- 39
- 54
3
votes
1 answer
Cakephp counterCache multiple counterScope - Logical issue
Problem:
I have two models: Dealer, Testdrive (Testdrive belongs to Dealer through dealer_id). I want to show real time statistics about the dealers: total (Testdrive.active = 1), processed (Testdrive.active = 1 && Testdrive.processed = 1) ...
I…

wappy
- 470
- 3
- 12
3
votes
2 answers
Counter cache for association of an association
I have a Forum model which has many discussions. Each discussion has many posts. Discussion is more or less just a collection of post objects.
Now I'd like to have a counter_cache available in Forum for the posts contained in that forums…

DVG
- 17,392
- 7
- 61
- 88
2
votes
0 answers
Rails ActiveRecord :counter_cache not updated if assocation not set up before create. Intentional?
I've implemented a belongs_to relation with :counter_cache => true and I notice that the counter cache does not get updated if the relation was not set up before the initial save.
For instance, say a company has_many employees. If I…

nioq
- 3,215
- 1
- 23
- 18
2
votes
0 answers
using :counter_cache with some conditions
I have the following model:
class EntityTag < ActiveRecord::Base
attr_protected :user_id, :post_id, :entity_id
belongs_to :user
belongs_to :post
belongs_to :entity
validates :user_id, :presence => true
validates :entity_id, :presence…

deruse
- 2,851
- 7
- 40
- 60
2
votes
2 answers
Rails Counter Cache with Active Storage
I have a Account model with 3 attachments, using Active Storage, has_many_attached :attachments.
I want to know how many attached files the account has, the most efficient way (aka no joins)
The only solution I found…

alagaesia
- 1,469
- 2
- 14
- 20
2
votes
2 answers
Is there a way to decrease in counter_culture gem when using column_names?
I need to decrease the counter of active users in a group, actually is increasing correctly when a new user pass from inactive to active in the group. But when change from active to inactive and then to active again the counter of active users is…

JUAN CAMPOS
- 31
- 6
2
votes
1 answer
Should I add an index on a counter-cache column in Ruby on Rails 3.0
Environment: Rails 3.0.4, MySQL, Ruby 1.8.7
I have the following table:
create_table "countries", :force => true do |t|
t.string "iso", :limit => 2, :null => false
t.string "name", :limit => 80, …

Andy
- 1,801
- 3
- 22
- 33