counter_cache not decrementing for has_many associations in ActiveReord

非 Y 不嫁゛ 提交于 2021-02-19 03:43:09
问题 My Rails 3 app has 2 models and a third that's join table between them and their has_many relationships. Basically, User and Show are joined by SavedShow, allowing users to save a list of shows: class Show < ActiveRecord::Base has_many :saved_shows has_many :users, :through => :saved_shows end class User < ActiveRecord::Base has_many :saved_shows has_many :shows, :through => :saved_shows end class SavedShow < ActiveRecord::Base belongs_to :user, :counter_cache => :saved_shows_count belongs_to

Rails 4: counter_cache in has_many :through association with dependent: :destroy

北战南征 提交于 2020-01-11 09:54:09
问题 Although similar questions have already been asked: counter_cache with has_many :through dependent => destroy on a "has_many through" association has_many :through with counter_cache none of them actually addresses my issue. I have three models, with a has_many :through association : class User < ActiveRecord::Base has_many :administrations has_many :calendars, through: :administrations end class Calendar < ActiveRecord::Base has_many :administrations has_many :users, through:

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

主宰稳场 提交于 2020-01-02 04:44:06
问题 I just implemented a number of custom counter_cache s using code like this: def after_save self.update_counter_cache end def after_destroy self.update_counter_cache end def update_counter_cache = Matching.where(:read => false).count end My question is this - what does the command => false) actually prevent beyond things like validates_with or before_validation ? Will my custom counter_caches be affected if I keep my

Rails counter cache with condition

大憨熊 提交于 2019-12-24 00:54:44
问题 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 is i only want count of tickets which are open or in progress state. I have this code it works fine normally but does not work with the condition can anyone guide me by letting me know how can i work it with conditions? Any help is appreciated

counter cache doesn't update but I can save to the parent and the child

寵の児 提交于 2019-12-12 15:06:20
问题 I added a counter cache but can't get it to update. But I can update the parent - the Blog Post model by adding a new blog post - and I can update the child - Comments model - by adding a new comment. The counter cache is supposed to keep track of the total number of comments per blog post by auto-updating the blog_posts.comments_count field. I'll outline some of the steps I went through and hopefully someone will notice something I did wrong. The schema dump is at the end. I have a Blog Post

How do I consistently increase a counter cache column?

元气小坏坏 提交于 2019-12-11 08:13:41
问题 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 race conditions may occur. Here is a quick explanation. Let's say my counter cache column is called foo_counts and its starting value is 0. If 2 web instance are loaded at the same time, both realize the count as 0. When it comes time to increase

Mongoid 3.1.0 CounterCache doesn't work

青春壹個敷衍的年華 提交于 2019-12-10 22:29:56
问题 I'm trying to use Mongoid CounterCache but it doesn't work. I tried to just use belongs_to :user, counter_cache: true But it returns Problem: Invalid option :counter_cache provided to relation :user. Summary: Mongoid checks the options that are passed to the relation macros to ensure that no ill side effects occur by letting something slip by. Resolution: Valid options are: autobuild, autosave, dependent, foreign_key, index, polymorphic, touch, class_name, extend, inverse_class_name, inverse

CakePHP 3: CounterCache with BelongsToMany

感情迁移 提交于 2019-12-08 02:35:56
问题 I have a BelongsToMany association, my tables are PostsTable , TagsTable and PostsTagsTable . As explained here in the CakePHP book (associations), I have this fields:, tags.tag, tags.post_count, posts_tags.tag_id, posts_tags.post_id Everything works correctly for now. So, as you can understand, now I want to use the tags.post_count field with CounterCache . I followed the CakePHP book, but I suppose this is a special case, in fact it doesn't work by simply adding the

Rails 3 and Rspec: counter cache column being updated to 2 when expected 1

☆樱花仙子☆ 提交于 2019-12-07 07:21:11
问题 I'm testing with Rspec a model named Solutions which has many Likes. Solution stores how many Likes it have (counter_cache). It has a "likes_count" attribute (and respective db field). When I create a Like record associated to a Solution, I expect that the solution attribute "likes_count" should be updated from nil to 1. When I do that in console, it works. But when I run the spec, doing the SAME THING I do in console, it update TWICE the "likes_count" field, setting it to 2. Take a look (in

:counter_cache => true for storing sum

◇◆丶佛笑我妖孽 提交于 2019-12-07 05:47:54
问题 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 to use this way for storing the sum of all scores in the column scores_count: :counter_cache => true But :counter_cache => true saving only the count of rows in the table scores . Is there any similar method for storing the sum of all values from