Ruby on rails 使用令牌统计api调用

Ruby on rails 使用令牌统计api调用,ruby-on-rails,api,authentication,token,Ruby On Rails,Api,Authentication,Token,我试图创建一个需要令牌才能使用的api。每次调用页面时,我都会检查控制器中是否存在带有此before_筛选器的令牌: def restrict_access_token authenticate_or_request_with_http_token do |token, options| ApiKey.exists?(access_token: token) end

我试图创建一个需要令牌才能使用的api。每次调用页面时,我都会检查控制器中是否存在带有此before_筛选器的令牌:

            def restrict_access_token
                authenticate_or_request_with_http_token do |token, options|
                    ApiKey.exists?(access_token: token)
                end
            end
当找到一个apikey时,我想在apikey表中增加一个count列

我在模型中寻找一个回调,当找到apikey时会触发,但在这种情况下似乎不起作用


任何想法?

在您的模型中,您需要覆盖
存在?
方法

def self.exists?(conditions = :none)
  if super
    ...{YOUR CALLBACK}...
  end
end
更新: 我更愿意自定义您的方法,如
find_和_increment(…)
。因此,在模型中:

def self.find_and_increment(conditions = :none)
  if object = where(conditions).first
    object.increment!(...)
  end
end