class User < ActiveRecord::Base
attr_accessible :username, :password
attr_accessor :username, :password
validates :username, :presence => true, :uniqueness => true
validates :password, :presence => true
end
测试:
it "prevents username duplication" do
FactoryGirl.create(:user, username: 'ted').should be_valid
FactoryGirl.build(:user, username: 'ted').should_not be_valid
end
我已经确认User.count()
是 2。唯一性约束我缺少什么?