Topic: checking to see is activated

So the short of it is I am implementing a simple email activation and have successfully been able to create an activation key in the column. The key is then replaced with null when the email is activated. I am having trouble with the logic to authenticate account activation. The model code is below...

def self.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil if user.nil?
    return false if user.activated != null
    return user if user.has_password?(submitted_password)
end

and the controller code is

def create
    user = User.authenticate(params[:session][:email], params[:session][:password])
    
    if user.nil?
      flash.now[:error] = "Invalid email/password combination."
      @title = "Sign in"
      render 'new'
  elsif user == 1
    flash.now[:error] = "The account you are attempting to log into has not been activated, please check your email."
    @title = "Sign in"
    render 'new'
  else
    sign_in(user,params[:session][:permanent])
    redirect_back_or dashboard_path
  end
  end

Re: checking to see is activated

Try something like this:

#define an exception class we can test for
class UserNotFound < Exceptions 
end

class User

  def self.authenticate(email, submitted_password)
      user = find_by_email(email)
      #raise exception if the user was not found
      raise UserNotFound unless user
      #raise exception if the user was found, but the password was wrong
      raise UserNotFound unless user.has_password?(submitted_password)
      user
  end
  
  def activated?
    activated.nil?
  end
  
end

def create
    begin      
      user = User.authenticate(params[:session][:email], params[:session][:password])
      #see if the user has activated their account
      if user.activated?
        sign_in(user, params[:session][:permanent])
        redirect_back_or dashboard_path
      else
        flash.now[:error] = "The account you are attempting to log into has not been activated, please check your email."
        @title = "Sign in"
        render 'new'
      end
    rescue UserNotFound
      #Houston we have a problem
      flash.now[:error] = "Invalid email/password combination."
      @title = "Sign in"
      render 'new'
    end
end