Questions tagged [counter-cache]

129 questions
44
votes
2 answers

Problem with counter_cache implementation

I'm getting 'rake aborted! ... posts_count is marked readonly' errors. I have two models: user and post. users has_many posts. posts belongs_to :user, :counter_cache => true I have a migration which adds the posts_count column to the users table…
NJ.
  • 2,155
  • 6
  • 26
  • 35
13
votes
0 answers

Readonly error when running a Rails 3 migration with update_attribute

I'm trying to add a counter_cache and this Rails 3 migration is giving me an error I just can't seem to resolve. The migration is class AddItemsCountToStore < ActiveRecord::Migration def self.up add_column :stores, :items_count,…
Adam Mikulasev
  • 615
  • 1
  • 5
  • 12
12
votes
1 answer

counter_cache in Rails on a scoped association

I have User model which has_many :notifications. Notification has a boolean column seen and a scope called :unseen which returns all notifications where seen is false. class User < ApplicationRecord has_many :notifications has_many…
GMA
  • 5,816
  • 6
  • 51
  • 80
9
votes
2 answers

counter_cache with conditions

I know I can use callbacks but this should be feasible. I did lengthly searching and no result. This is what I thought would have worked. def User < ActiveRecord::Base has_many :documents has_many :draft_docs , :class_name => 'Document',…
9
votes
2 answers

Track sum of some fields in the association - "sum_cache"

I have tables 'orders' and 'items' with has_many association in the model. class Order < ActiveRecord::Base has_many :items class Item < ActiveRecord::Base belongs_to :order Item consists of 'quantity' field, and Order consists of 'quantity_sum'…
jayandra
  • 296
  • 2
  • 8
8
votes
1 answer

Rails 4 counter cache out of sync

I have a Rails 4.2 app with a counter cache (setup with belongs_to :my_model, counter_cache: true) that is setting incorrect values. I can literally call up the console and call MyModel.associated_model_count and get 20, and then call…
7
votes
2 answers

counter_cache in single table inheritance

I am wondering if the counter_cache would work in single table inheritance. For these models: class User has_many :questions end class Question belongs_to :user, :counter_cache => true end class SimpleQuestion < Question end class…
7
votes
1 answer

Carrierwave + S3 Storage + Counter Cache Taking too Long

I have a simple app that receives POSTed images via an API and sends them to S3, via Carrierwave. My Photos table has a counter_cache as well. 80% of the time my transaction time is HUGE, like 60 seconds or more, and more than 90% of this time is…
Rafael Oliveira
  • 2,823
  • 4
  • 33
  • 50
7
votes
2 answers

Rspec testing of counter_cache column's returning 0

For days now I have been trying to get to the bottom of what seam to be something that should be very easy to do... I am however still very new to the world of rails and ruby and I just cant work this one out... :p Anyway the problem I am having is…
utx0_
  • 1,364
  • 14
  • 24
7
votes
2 answers

:counter_cache => true for storing sum

I have the table users and scores. Here are the associations: belongs_to :user #score model has_many :scores #user model The table users has the column called scores_count. In this column I store the sum of all values in the table scores. I wanted…
user984621
  • 46,344
  • 73
  • 224
  • 412
6
votes
1 answer

Rails 3: Update counter_cache only if published

This is a tricky question. I have a project with multiple content types, such as Article, Review, etc. Each content type has comments, comments are polymorphic as they can belong to any content type. I want to put a total comments counter on all my…
Shannon
  • 2,744
  • 3
  • 28
  • 37
6
votes
2 answers

What is covered by save(:validate => false)?

I just implemented a number of custom counter_caches using code like this: def after_save self.update_counter_cache end def after_destroy self.update_counter_cache end def update_counter_cache self.company.new_matchings_count =…
sscirrus
  • 55,407
  • 41
  • 135
  • 228
6
votes
3 answers

Ruby on Rails -- Is a counter-cache transaction safe?

Will it handle the two people updating problem ok? I googled and looked in the api but found nothing Rails 3+, ruby 1.9.3
5
votes
4 answers

Ruby on Rails counter cache error

I get the error Unknown key(s): counter_cache when trying to implement a counter cache column in my RoR app. I implemented the model associations in this question: Model association question Here's my migration: class AddVideoVotesCountToVideos <…
Justin Meltzer
  • 13,318
  • 32
  • 117
  • 182
5
votes
1 answer

Rails counter cache with condition

I have a rails hotel application which has rooms inside it. Rooms can have n number of tickets associated to them. I have create a counter cache with counter culture gem which updates the room table with number of tickets assigned to it, the problem…
Mohammed Sayer
  • 131
  • 1
  • 9
1
2 3
8 9