2010-02-22 84 views
0

我想跟蹤用戶登錄到我的網站(這是Rails應用程序)的次數。有沒有其他的電話像「created_on or updated_on」那樣可以在我的模型中製作一個跟蹤這類信息的小計數器?目前我正在使用restful-authentication。rails - track用戶登錄數

回答

3

我會login_count字段添加到您的User/Account模型。然後在User/Account模型改變這個方法:

def self.authenticate(login, password) 
    return nil if login.blank? || password.blank? 
    u = find_by_login(login) # need to get the salt 
    u && u.authenticated?(password) ? u.increase_login_count : nil 
end 

這種方法添加到模型:

def increase_login_count 
    self.login_count += 1 
    self.save 
    self 
end 
+0

是否有任何理由,這將是最好的盧卡斯的答案嗎? – ChrisWesAllen 2010-02-22 20:58:12

+0

我發現它更好,因爲一切都停留在模型中,而不是控制器中。這將有助於您的數據庫保持一致。 – klew 2010-02-22 21:07:14

2

您可以創建在用戶表中調用login_count什麼的一列,然後在SessionsController.create方法

if user 
    user.login_count += 1 
    user.save(false)  #update without validations. 

    # .... other RestfulAuthentication generated code ....