在我更新控制器中用户的属性后,我的模型cropping
方法在循环中被调用,该循环永远不会结束。
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
一旦设置了crop_x、crop_y、crop_w 和crop_h,该cropping
方法将始终返回true,这将继续调用该reprocess_avatar
方法。这可能是由于reprocess_avatar
方法也在更新avatar
用户表的属性。所以再次after_update
触发导致循环。
有没有办法在更新后只调用一次该方法?