Questions tagged [counter-cache]
129 questions
1
vote
1 answer
ActiveRecord counter_cache increments db but not instance
I have two simple classes Company and Votings which I test with rspec.
When I add a voting to a company it gets counted by an activeRecord
class Company < ActiveRecord::Base
attr_accessible :name, :votings_count
has_many :votings, :dependent =>…

Jan
- 12,992
- 9
- 53
- 89
1
vote
1 answer
rails counter cache while destroy
For example, I have three model user, question and answer, and the relationship between them are:
class User < ActiveRecord::Base
has_many :answers
has_many :questions
end
class Question < ActiveRecord::Base
has_many :answers, :dependent =>…

placeintime
- 31
- 5
1
vote
0 answers
How can I make counter cache with complicated condition?
I migrated this to enable counter cache
class AddUniquecodesCountToUser < ActiveRecord::Migration
def up
add_column :users, :uniquecodes_count, :integer, :default => 0
User.reset_column_information
User.all.each do |p|
…

MKK
- 2,713
- 5
- 31
- 51
1
vote
1 answer
How can I ensure that counter_cache is updated correctly in rails unit tests?
I've got a normal counter_cache set up for my Rails 3.2.6 model. It works flawlessly in the webapp, but fails in unit tests.
The following snippet illustrates the issue at hand:
test "counter_cache ..." do
u = FactoryGirl.create(:customer)
…

user569825
- 2,369
- 1
- 25
- 45
1
vote
2 answers
How do I consistently increase a counter cache column?
Lets say I have a counter cache that needs to to be incremented on every page load. Say I have 10 web instances. How do I consistently increase a counter cache column?
Consistency is easy with one web instance. but with several instances running, A…

Christian Fazzini
- 19,613
- 21
- 110
- 215
0
votes
1 answer
counter_cache not incrementing... what am I doing wrong?
This seems super simple and so is making my life miserable. Rails 3.1.3.
Edit: Based on advise below added t.integer "versions_count", :default => 0 to the migration,rolled back, recreated the database. Now the versions_count stays at 0.…

aaandre
- 2,502
- 5
- 33
- 46
0
votes
1 answer
How to trigger multiple counter_cache in one model as has_many :through
I have 3 models(Allen, Bob, Chris) which are polymorphic with Join model. and a User model which connect with the join model.
class Allen < ActiveRecord::Base
has_many :joins, :as => :resource
...
end
class Bob < ActiveRecord::Base
has_many…

chenillen
- 1
- 1
0
votes
1 answer
when I do order on a counter_cache it returns nil objects first?
I have owns_count counter_cache on my items model. When i do
Items.order("owns_cache DESC")
, it returns me objects that are nil before other results. If I do
"owns_cache ASC"
, it is correct.
What should I be doing?

Julien
- 185
- 1
- 2
- 15
0
votes
1 answer
Getting couter_cache error when saving record in rails
I'm getting an error when trying to create a vote on items in my rails application. It seems to be related to the counter cache.
error
#

Antarr Byrd
- 24,863
- 33
- 100
- 188
0
votes
1 answer
batch video views to redis instead of directly to the db
The idea is to move video views counters from inserting directly to the db to Redis using INC/HINCRBY, and, every x sec get a batch of them and insert in one go to the db. As I see it, there are 2 options:
set key per id and INC
use hash and…

Nir
- 2,497
- 9
- 42
- 71
0
votes
2 answers
In cakephp - can I counterCache related models? eg: User -> Profile
I have a field member_count on my countries table.
Profile belongsTo Country
counterCache => 'member_count',
counterScope => array( 'Profile.country => 'US' )
So, when a user runs a save on $this->User->Profile->save( $this->data), i would like to…

Barry Chapman
- 6,690
- 3
- 36
- 64
0
votes
1 answer
What is the best way to store a multi-dimensional counter_cache?
In my application I have a search_volume.rb model that looks like this:
search_volume.rb:
class SearchVolume < ApplicationRecord
# t.integer "keyword_id"
# t.integer "search_engine_id"
# t.date "date"
# t.integer "volume"
belongs_to…

amoks
- 123
- 9
0
votes
0 answers
Rails reset_counters in counter_cache with conditions
I've two counter_cache, one for total number of campaign_codes and one only when campaign_code was used - campaign_codes.where(state: 'used'). To update total number of campaign_codes in the rails c I used script…

mr_muscle
- 2,536
- 18
- 61
0
votes
1 answer
How trigger counter cache after destroy_all
I have to destroy a big list of data, and update my counter cache, but when I user destroy_all counter cache is called at every delete. Is there any way to update my counter only after the destroy_all?
I already tried ActiveModel Callbacks, but they…

Eduardo
- 37
- 1
- 9
0
votes
1 answer
cakephp Counter cache in nested model
i have nested model structure
id
parent_id
counter
name
how to use automated counter cache

baur79
- 273
- 5
- 18