Questions tagged [counter-cache]
129 questions
1
vote
1 answer
Rails counter cache with has_many: :through
I have a Part model that has_many :images, through: :gallery and I would like to implement counter_cache to store the images count of a part. This is my setup.
Part
has_one :gallery, dependent: :destroy
has_many :images, through:…

jedi
- 2,003
- 5
- 28
- 66
1
vote
2 answers
Avoiding individual database calls for count
My models look like this:
class Movie < ActiveRecord::Base
attr_accessible :title, :year, :rotten_id, :audience_score,
:critics_score, :runtime, :synopsis, :link, :image
has_many :jobs, :dependent => :destroy
has_many :actors, :through =>…

leflings
- 646
- 3
- 17
1
vote
0 answers
counter_cache increments twice
Rails 3.2.14
Ruby 2.1.7
When using counter_cache, the count column gets incremented by 2 instead of 1. For example:
class Finger < BaseModel
belongs_to :hand, :counter_cache => true
end
class Hand < BaseModel
has_many :fingers
end
So if I…

bjacobs
- 401
- 1
- 6
- 17
1
vote
1 answer
CakePHP 3 CounterCache - don't start counting with 0
I'm trying to migrate a IP-based voting system to logged-in-user-based voting system and would like to keep the old votes/likes. So i copied the old systems like_count into a column in my quotes table.
quotes table
votes table
I implemented…

ndru
- 83
- 1
- 10
1
vote
4 answers
How to display Parent record with total number of its Child record in rails
class Attachment < ActiveRecord::Base
belongs_to :user, foreign_key: :creator_id
belongs_to :deal_task, foreign_key: :relation_id
end
class DealTask < ActiveRecord::Base
has_many :attachments, foreign_key: :relation_id
end
I have parent…

Ketan Mangukiya
- 360
- 1
- 7
1
vote
2 answers
Rails Active Record - counter_cache to return records only from a given period of time
I have a User who has_many articles. I use a counter cache to store the number of articles ever published by a user.
class User < ActiveRecord::Base
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :user, :counter_cache =>…

Joanna Gaudyn
- 597
- 3
- 21
1
vote
2 answers
How to test counter_cache with test::unit and mocha
I'm curious if i can mock out somehow the testing of a counter cache.
Now I'm creating all the records to test the counter_cache. Is there any good practices to test counter cache effectively?
Edit:
I'm just interested in testing that, the counter…

dombesz
- 7,890
- 5
- 38
- 47
1
vote
1 answer
Getting count of entries for a sub-model with date filtering in Rails
I want to get count of objects for a sub-model in Rails with date filtering.
Let's explain in detail:
Assume that I have two models.
Post
Comment
Also the Comment model belongs to Post model
I want to get comments count for each post by date…
user6186516
1
vote
0 answers
Counter_culture for has_one relationship (rails)
I use counter_culture gem, but when I want to use it, it throws an error that it cannot find product_id field in Product model, which makes sense because it should look for 'id'.
Below is the query generated when I call…

Miroslav
- 158
- 2
- 22
1
vote
1 answer
Rails and optimistic locking: creating an object through an association changes lock version in DB, but not in ruby object
My projects have many boilerplate copies.
class Project < ActiveRecord::Base
has_many :boilerplate_copies, dependent: :destroy
end
class BoilerplateCopy < Boilerplate
belongs_to :project, counter_cache: true
belongs_to…

Joshua Muheim
- 12,617
- 9
- 76
- 152
1
vote
2 answers
Rails: How to increment counter_cache?
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::: SOLVED :::
Was able to figure out counter_cache on the Vote model and use it in Topics. Updated question below. Once…

sf2k
- 592
- 2
- 6
- 25
1
vote
1 answer
Undefined method `reset_counters' for Mongoid 3.1.6
I just updated my mongoid to version 3.1.6, so that I could use the reset_counters method to keep track of my model relations. But just as I did before the gem update I still get this error:
undefined method `reset_counters' for…

Cjoerg
- 1,271
- 3
- 21
- 63
1
vote
1 answer
Is it possible to track something else with a counter_cache other than the number of objects in an association?
All I am trying to do, is in an easy/light way...whenever the PostsController#Show action is fired, to increment the post.views or post.view_count column. I don't want to have to do anything too heavy.
It feels like the native counter_cache would be…

marcamillion
- 32,933
- 55
- 189
- 380
1
vote
2 answers
Rails - Order by counter cache (likes_count), but only with likes created today
Currently, I am ordering records with counter cache that keeps track of the amount of likes they have, such as:
Post.order("COALESCE(likes_count, 0) DESC").limit(10)
Is there a way to incorporate a time limit on the likes of the cache that are used…

Kasperi
- 853
- 7
- 17
1
vote
1 answer
How to exclude increasing counter_cache on some state changes?
ShiftNote
belongs_to :shift, counter_cache: true
workflow do
state :scheduled
state :canceled
end
Shift
has_many :shift_notes
scope :opened, lambda {
locked
.where("shift_notes_count < shifts.limit")
}
How to not…

tomekfranek
- 6,852
- 8
- 45
- 80