2014-02-19 136 views
0

我试图创建一个需要使用令牌的API。每次页面被称为我是否与此的before_filter控制器存在的令牌:用令牌计算API调用

  def restrict_access_token 
       authenticate_or_request_with_http_token do |token, options| 
        ApiKey.exists?(access_token: token) 
       end 
      end 

当apikey发现我想要增加在ApiKey表计列。

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

任何想法?

回答

1

在你的模型,你需要重写exists?方法

def self.exists?(conditions = :none) 
    if super 
    ...{YOUR CALLBACK}... 
    end 
end 

更新: 我宁愿来定制你的方法一样find_and_increment(...)。所以在模型中:

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

我该如何访问那里的对象? self.count不起作用 – Inzajt

+0

我已更新答案 – Zakwan

+0

很好,谢谢! – Inzajt