假设!object.nil? == object.present?
在 Rails 中是安全的,还是有陷阱?这是一个场景:
def signed_in?
!current_user.nil? # Would current_user.present? mean the same thing here?
end
def sign_in(user)
cookies[:token] = user.token
self.current_user = user
end
def current_user
@current_user ||= User.find_by(token: cookies[:token]) if cookies[:token]
end