Questions tagged [counter-cache]
129 questions
0
votes
0 answers
Counter cache not working in rails when creating a new value from worker
I have a rails app where I would like to use counter cache and before implementing into the app I tried in a sample app to check how it works and it worked so good. But I cannot make it work in the app that I were required to do.
The difference in…

logesh
- 2,572
- 4
- 33
- 60
0
votes
1 answer
Deeply associated column_names in counter_culture sum
When defining column_names in a counter_culture method, is it possible to deeply associate? In the docs and examples its always an attribute belonging to the model that is used to determine column_names. But, what if the attribute belongs to an…

hellion
- 4,602
- 6
- 38
- 77
0
votes
1 answer
Strange Behaviour of Counter Cache In Spec
I have a standard counter cache set up on Gallery to track its Photos. The counter cache is named as per Rails default photos_count.
In my Gallery spec I test the counter cache like this:
it "updates 'photos_count' counter cache" do
gallery =…

Undistraction
- 42,754
- 56
- 195
- 331
0
votes
1 answer
How to disable counter cache in polymorphic has_one association on Rails?
I'm needing to disable counter cache for has_one associations as below and keep it just on has_many:
class User < ActiveRecord::Base
has_one :email, as: :emailable
end
class Company < ActiveRecord::Base
has_many :emails, as:…

leompeters
- 886
- 1
- 13
- 24
0
votes
1 answer
Counter Cache on a condition not on association
I've read on counter caches on associations.
Is there a way to easily implement (via a gem that will do the heavy lifting) of a counter cache for some condition? for example:
usually a counter cache would be
class User
has_many :messages
class…

Nick Ginanto
- 31,090
- 47
- 134
- 244
0
votes
1 answer
ActiveRecord counter cache update
I'm having a problem with counter_cache. Say I have three models. User and Article have many ArticleUpvotes. User can create ArticleUpvote for an article.
User
# models/user.rb
class User < ActiveRecord::Base
has_many :upvotes, class_name:…

Igor Pantović
- 9,107
- 2
- 30
- 43
0
votes
2 answers
Conditions on rendering @users (exclude current_users and users with friend association)
Currently, I'm doing the conditions in the view. I need to do it in model or controller, but haven't found a way to do it that works.
I have a view rendering 10 users via a partial that sorts them by their friends count (all examples…

Kasperi
- 853
- 7
- 17
0
votes
1 answer
Using decrement_counter in after_update callback is subtracting 2?
I have three models. Employer, User, Job.
class Employers
has_many :jobs
has_many :users, through: :jobs
end
class User
has_many :jobs
end
class Job
belongs_to :user
belongs_to :employer
end
The Job model has a boolean…

hellion
- 4,602
- 6
- 38
- 77
0
votes
1 answer
Rails: rake db:migrate failure `verify_readonly_attribute`
This migration worked fine on an initial developer machine but is failing in a second dev environment. Setups are identical as far as we can tell.
Migration:
class AddInventoryItemsCountToProduct < ActiveRecord::Migration
def change
add_column…

Meltemi
- 37,979
- 50
- 195
- 293
0
votes
1 answer
Rails: best way to turn these requests into a counter_cache
I've got several of these requests each page load:
SELECT COUNT(*) FROM `impressions` WHERE `impressions`.`impressionable_id` IN (229) AND `impressions`.`impressionable_type` = 'Document' AND (YEAR(created_at) = 2013 && MONTH(created_at) = 8 &&…

t56k
- 6,769
- 9
- 52
- 115
0
votes
1 answer
Counter cache doesn't get updated - not sure why
I have a stores application on github. I'm trying to implement counter_cache for two models, 1. Divisions model and 2. Products model. For some reason I'm not sure of the counter cache(divisions_count) doesn't get incremented automatically for the…

boddhisattva
- 6,908
- 11
- 48
- 72
0
votes
2 answers
Does counterCache suffer from race conditions?
I have a question about counterCache that the documentation doesn't clarify at all.
Does counterCache checks race conditions when updating the field value?
For example, let's say we have a forum implementation, and for each forum, we have a number n…

Roberto Maldonado
- 1,585
- 14
- 27
0
votes
0 answers
Rails counter_cache screws up Ajax call that uses empty?
I have a page where a User can see their deliveries (like a notification). On that page they could delete a single delivery by clicking on an X. That was linked with the following controller actions
deliveries_controller.rb
def destroy
@id =…

dewyze
- 979
- 1
- 7
- 21
0
votes
2 answers
What's the most efficient way to keep track of an accumulated lifetime sales figure?
So I have a Vendor model, and a Sale model. An entry is made in my Sale model whenever an order is placed via a vendor.
On my vendor model, I have 3 cache columns. sales_today, sales_this_week, and sales_lifetime.
For the first two, I calculated it…

marcamillion
- 32,933
- 55
- 189
- 380
0
votes
1 answer
remove record when counter cache reached 0
I have the following model with the following relations:
+------+ 1 n +------------+ n 1 +-----+
| Post |--------| TagMapping |--------| Tag |
+------+ +------------+ +-----+
Now, in my application the Post count of a Tag is…

MarcDefiant
- 6,649
- 6
- 29
- 49