Counter cache not working in rails when creating a new value from worker

86 views Asked by At

I have a rails app where I would like to use counter cache and before implementing into the app I tried in a sample app to check how it works and it worked so good. But I cannot make it work in the app that I were required to do.

The difference in two apps is the create being called from the controller in the app that works with counter_cache whereas the create is called from the worker on the app that does not work.

So do we need to create the record only from the controller to get it working or will it be some other error?

Please help me. Thanks in advance.

Code Samples

class Post < ActiveRecord::Base
    has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post, :counter_cache => true
end

In my comments controller I have

@comment = Comment.create(comment_params)

and in my views I have

post.comments_count

the above code works whereas

Comment.create(comment_params)

in sidekiq worker which is called as async does not work i.e does not increment the count in table.

0

There are 0 answers