Excess errors on model from somewhere

Posted by gmile on Stack Overflow See other posts from Stack Overflow or by gmile
Published on 2010-04-12T12:40:11Z Indexed on 2010/04/12 12:43 UTC
Read the original article Hit count: 232

I have a User model, and use an acts_as_authentic (from authlogic) on it. My User model have 3 validations on username and looks as following:

User < ActiveRecord::Base

  acts_as_authentic

  validates_presence_of   :username    
  validates_length_of     :username, :within => 4..40 
  validates_uniqueness_of :username

end

I'm writing a test to see my validations in action. Somehow, I get 2 errors instead of one when validating a uniqueness of a name. To see excess error, I do the following test:

describe User do
  before(:each) do
    @user = Factory.build(:user)
  end

  it "should have a username longer then 3 symbols" do
    @user2 = Factory(:user)
    @user.username = @user2.username
    @user.save

    puts @user.errors.inspect
  end
end

I got 2 errors on username: @errors={"username"=>["has already been taken", "has already been taken"]}.

Somehow the validation passes two times. I think authlogic causes that, but I don't have a clue on how to avoid that.

Another case of problem is when I set username to nil. Somehow I get four validation errors instead of three: @errors={"username"=>["is too short (minimum is 3 characters)", "should use only letters, numbers, spaces, and .-_@ please.", "can't be blank", "is too short (minimum is 4 characters)"]}

I think authlogic is one that causes this strange behaviour. But I can't even imagine on how to solve that. Any ideas?

© Stack Overflow or respective owner

Related posts about validation

Related posts about rails