Issues with Rails built-in cache counter
seivan opened this issue · 2 comments
belongs_to :order, :counter_cache => :contributors_count
contributors_count is null in the response, but 1 in the model
Next request. It's 1 in the response and 2 in the model.
I've resorted to callbacks for this for now. But really like this out of the box.
Basically the attribute isn't set when the response is sent.
Hey :)
I am able to reproduce you problem, but this is not a problem of acts_as_api but of the Rails counter_cache implementation.
The same behaviour appears when you use the common rendering methods of Rails:
class ItemsController < ApplicationController
def create
@user = User.find(params[:user_id])
@item = @user.items.build(params[:item])
respond_to do |format|
if @item.save
format.xml { render :xml => @user, :status => :created }
else
format.xml { render :xml => @item.errors, :status => :unprocessable_entity }
end
end
end
end
I think the problem is that @item.save
will modify the user model in the after_create
callback but ActiveRecord
won't know its instances of the user are dirty now (@user.changed?
returns false). If you reload the @user
instance in the case above, it will show the correct count.
Closing this for now.