Reputation: 23354
My model's cropping
method is called in a loop which never ends after I update the attributes of user in the controller.
User controller code
-
def change_img
@user = current_user
#this triggers the model's after_update callback
@user.update_attributes(params[:user])
flash[:notice] = "Successfully updated Image."
render :action => 'crop'
end
User Model code
-
after_update :reprocess_avatar, :if => :cropping?
def cropping?
#this method is called infinitely why?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
Once the crop_x, crop_y, crop_w and crop_h are set, the cropping
method will always return true which will keep on calling the reprocess_avatar
method. This is probably due to the fact that reprocess_avatar
method is also updating avatar
attribute of user table. And so again after_update
triggers causing a loop.
Is there any way to call the method only once after update?
Upvotes: 2
Views: 1742
Reputation: 23354
I resolved the issue by removing the after_update
from model and making a call from controller's function itself.
def change_img
@user = current_user
@user.update_attributes(params[:user])
if([email protected]_x.blank? && [email protected]_y.blank? &&
[email protected]_w.blank? && [email protected]_h.blank?)
@user.avatar.reprocess!
end
flash[:notice] = "Successfully updated Image."
render :action => 'crop'
end
Thanks!
Upvotes: 7
Reputation: 5688
if the reprocess_avatar is updating something, make sure you are using any non-callback update method so you don't trigger any further callbacks in the object. If, for instance, you are settings flags or updating some id column or setting timestamps on your DB representation, use some straight-to-db method like #update_column or #touch. But without seeing what your reprocess_avatar method actually makes it hard to give a better advice.
Upvotes: 0