views:

37

answers:

1

I have a simple User class with the following validation of name uniqueness:

class User < ActiveRecord::Base

  validates :name, :uniqueness => true,

It works great when a new user is created. However, when I check the login form, the user enters his name, and the system says it's already taken which doesn't make any sense.

So I implemented a separate valid_login? method, however I can't turn that unqueness check there:

def valid_login?
  validates :name, :uniqueness => false # doesn't work
end

This is my controller's code:

def login
    return unless request.post?

    @user = User.new(params[:user])
    if @user.valid_login?
      # Redirect to user's page
    end
end

I'm using my own authentication system which is quite simple: I store user's ID + password's hash in the cookies.

How can I turn of certain validation when I don't need it?

A: 

I solved this problem with the :if/:unless parameters.

Alex