counter-cache

CakePHP 3: CounterCache with BelongsToMany

自作多情 提交于 2019-12-06 11:52:17
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.id, tags.tag, tags.post_count posts_tags.id, 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 behavior to the PostsTable . Can you help me? Thanks. From CakePHP Book The CounterCache behavior works

How to implement a Counter Cache in Rails?

☆樱花仙子☆ 提交于 2019-12-06 04:47:22
问题 I have a posts controller and a comments controller. Post has many comments, and comments belong to Post. The associate is set up with the counter_cache option turned on as such: #Inside post.rb has_many :comments #Inside comment.rb belongs_to :post, :counter_cache => true I have a comments_count column in my posts table that is defaulted to zero, as such: add_column :posts, :comments_count, :integer, :default => 0 In the create action of my comments controller, I have the following code: def

Rspec testing of counter_cache column's returning 0

霸气de小男生 提交于 2019-12-06 00:02:25
问题 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 that I have a number of :counter_cache columns in my model's, which are all working quite nicely when testing them manually. However I am wanting to do the TDD thing and I cant seam to test them in rspec for some unknown reason?? Anyway here is an

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

不羁的心 提交于 2019-12-05 12:43:47
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 console) WORKING : irb(main):001:0> solution = Factory(:solution) irb(main):004:0> solution.likes_count

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

与世无争的帅哥 提交于 2019-12-05 11:11:12
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 self.company.new_matchings_count = Matching.where(:read => false).count self.company.save end My question is this - what does the command Model.save(:validate => false) actually prevent beyond things like validates_with or before_validation ? Will my custom counter_caches be affected if I keep my existing saves without validation? If you pass in the :validate=>false, it skips the valid? command.

Rails has_many through polymorphic counter cache

心已入冬 提交于 2019-12-05 05:19:19
问题 I have two models I link together using a polymorphic has_many through association and I would like to add a counter_cache but it seems Rails/ActiveRecord does not support this feature out of the box. class Classifiable < ActiveRecord::Base has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id end class Taxonomy < ActiveRecord::Base has_many :classifications, :as => :taxonomy, :foreign_key => :taxonomy_id end class Question < Classifiable has_many :categories,

How to implement a Counter Cache in Rails?

我们两清 提交于 2019-12-04 10:53:48
I have a posts controller and a comments controller. Post has many comments, and comments belong to Post. The associate is set up with the counter_cache option turned on as such: #Inside post.rb has_many :comments #Inside comment.rb belongs_to :post, :counter_cache => true I have a comments_count column in my posts table that is defaulted to zero, as such: add_column :posts, :comments_count, :integer, :default => 0 In the create action of my comments controller, I have the following code: def create @posts = Post.find(params[:post_id]) @comment = @post.comments.build(params[:comment]) if

Rspec testing of counter_cache column's returning 0

左心房为你撑大大i 提交于 2019-12-04 04:22:24
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 that I have a number of :counter_cache columns in my model's, which are all working quite nicely when testing them manually. However I am wanting to do the TDD thing and I cant seam to test them in rspec for some unknown reason?? Anyway here is an example of my model's (User's, comments & Media): class User < ActiveRecord::Base has_many :comments

Rails has_many through polymorphic counter cache

此生再无相见时 提交于 2019-12-03 20:22:50
I have two models I link together using a polymorphic has_many through association and I would like to add a counter_cache but it seems Rails/ActiveRecord does not support this feature out of the box. class Classifiable < ActiveRecord::Base has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id end class Taxonomy < ActiveRecord::Base has_many :classifications, :as => :taxonomy, :foreign_key => :taxonomy_id end class Question < Classifiable has_many :categories, :through => :classifications, :as => :classifiable, :source => :taxonomy, :source_type => "Category" end

Problem with counter_cache implementation

你离开我真会死。 提交于 2019-12-03 00:53:08
问题 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 and then calculates and records the current number of posts per user. self.up add_column :users, :posts_count, :integer, :default => 0 User.reset_column_information User.all.each do |u| u.update_attribute( :posts_count, u.posts.count) end end when I