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 implement开发者_开发百科ed 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?
I solved this problem with the :if/:unless parameters.
精彩评论