我已经定义了一个回调 after_find,用于根据检索到的模型实例检查一些设置。如果未完成设置,我不希望从 find 方法返回实例。那可能吗?
控制器的示例如下:
class UtilsController < ApplicationController
def show
@util = Util.find(params[:id])
end
end
该模型:
class Util < ActiveRecord::Base
after_find :valid_util_setting
def valid_util_setting
# calculate_availability? complex calculation
# that can not be part of the sql statement or a scope
unless self.setting.calculate_availability?(User.current.session)
#if not available => clear the record for view
else
#nothing to do here
end
end
end