Questions tagged [counter-cache]

129 questions
0
votes
1 answer

Starting a counter cache from greater than 0

I have records that has_many objects. The objects have already been created with the record_id column prepopulated with a future record.id in mind. However when that future record is created, the counter cache always starts at 0. How can I make the…
ecki
  • 780
  • 1
  • 7
  • 20
0
votes
1 answer

Cakephp 3: Countercache with conditions on the target model

I am trying to count the number of Spots associated with a Plan, but limited to Spots downloaded after the plans renewal date. Hope that makes sense. I would image something like this, but it doesn't work: class SpotsTable extends Table { public…
0
votes
0 answers

reset_counters not works for not association field

https://api.rubyonrails.org/v5.0.1/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-reset_counters I have a field called sequence in my model. I want to reset all sequences of particular record set and update all sequences one by one 1.…
Ankita Agrawal
  • 422
  • 5
  • 20
0
votes
1 answer

Counter cache cross settings in Ruby on Rails

I have following models: Category, SubCategory, Product, User and CategoryPerUser. Below is corresponding ER diagram: Here is a brief explanation of what I want to achieve: a product belongs to a sub_category which belongs to a category a user can…
smndiaye
  • 438
  • 7
  • 13
0
votes
1 answer

Sum of counts for nested models

I have two models: Category and Ad. Category has_many :ads + I added counter cache :ads_count. I use gem awesome_nested_set to make nested categories, so Category1 can be parent/child of Category2 which can be parent of Category3 etc. What I need…
user7010020
0
votes
1 answer

Error when trying to add counter_cache to a self-join rails model

class Product < ActiveRecord::Base belongs_to :parent, :class_name => 'Product', :foreign_key => :parent_id has_many :children, :class_name => 'Product', :foreign_key => :parent_id I am trying to add a counter cache to the :children…
oktober
  • 153
  • 2
  • 8
0
votes
1 answer

ROR: counter cache, has_many thougth, delete with nested params

I have next models: class Document < ActiveRecord::Base has_many :sub_roles_documents, dependent: :destroy has_many :sub_roles, through: :sub_roles_documents,class_name: '::SubRole' end class SubRole < ActiveRecord::Base has_many…
kunashir
  • 917
  • 8
  • 21
0
votes
1 answer

CakePHP 3 CounterCache not updating on delete with belongsToMany SelfJoinTable

CakePHP vertion: 3.3.11 CounterCache working on add method but not working on delete method. SentenceTable $this->belongsToMany('Sentences', [ 'foreignKey' => 'second_sentence_id', 'targetForeignKey' => 'sentence_id', …
Kani
  • 135
  • 1
  • 12
0
votes
1 answer

Ruby on Rails - Counter cache doesn't work on production

I have added counter_cache to punching_bag gem. Everything works fine in Development ENV, but in Production ENV it doesn't do what it suppose to to. I can see that punches_countcolumn is added into my posts table, but it doesn't increment the…
Rubioli
  • 685
  • 6
  • 34
0
votes
0 answers

Why does the .size method not respond with the actual value stored in my counter_cache?

I have a Question model that has_many :answers & # answers_count :integer default(0) On my Answer model, I have: belongs_to :question, counter_cache: true On my Question#Show page though, when showing my Answer#Form partial, I build an instance of…
marcamillion
  • 32,933
  • 55
  • 189
  • 380
0
votes
2 answers

Rails Counter Cache On the same model?

I have a model Task, and each task has_many other tasks: Class Task < ActiveRecord::Base belongs_to :sub_task, class_name: Task.name, touch: true has_many :sub_tasks, class_name: Task.name, foreign_key: :sub_task_id, dependent:…
Yossale
  • 14,165
  • 22
  • 82
  • 109
0
votes
1 answer

Conditional counter cache dependent on Paperclip

I have a counter_culture counter that depends on whether a Paperclip attachment is defined or not: class Post < ::ActiveRecord::Base belongs_to :user counter_culture :user, column_name: Proc.new { |p| p.media? ? 'posts_with_photo_count' : nil…
mrstif
  • 2,736
  • 2
  • 27
  • 28
0
votes
1 answer

How to order by counter_cache using only data from the past 30 days

I am trying to rank a list of programs by number of times they have been viewed. Right now, I have a Program model and a Views model that belongs_to program. In Views.rb I create a counter_cache: belongs_to :program, counter_cache: :views_count I…
0
votes
1 answer

CakePHP counterCache in belongsTo (HABTM)

i have the following Model structure. Post id, title, text, etc. Tag id, name, posts_count, created tags_posts id, tag_id, post_id, created Tag Model: public $hasAndBelongsToMany = array( 'Post' => array( 'className'…
0
votes
2 answers

Should I use the class or instance method to increment my object?

There are two methods to increment an attribute in Rails: Instance-level: http://apidock.com/rails/ActiveRecord/Base/increment! Class-level: http://apidock.com/rails/ActiveRecord/Base/increment_counter/class I want to update a counter attribute on…
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
1 2 3
8 9