1
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。唯一性约束我缺少什么?

4

1 回答 1

0

attr_accessor 破坏模型。摆脱那条线。

于 2012-12-20T20:16:30.313 回答