开发者

rails -- track number of user login

开发者 https://www.devze.com 2022-12-21 02:30 出处:网络
I\'d like to track how many times a user logs in to my site which is a Rails app.Is there any other call like \"created_on or updated_on\" that can makea little counter in my model that tracks that ki

I'd like to track how many times a user logs in to my site which is a Rails app. Is there any other call like "created_on or updated_on" that can make a little counter in my model that tracks that kind of info? I'm usi开发者_StackOverflow中文版ng restful-authentication currently.


I would add login_count field to your User/Account model. Then change this method in User/Account model:

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

and add this method to model:

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


You could create a column in the user table called login_count or something and then in the SessionsController.create method

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

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

精彩评论

暂无评论...
验证码 换一张
取 消