0

User次のようなモデルの Rails アプリケーションがあります。

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  field :email, type: String
  index({ email: 1 }, { unique: true })

  validates :email, presence:true, uniqueness:true
end

それが私のテストです:

describe User do
  it 'has an unique email' do
    first_user = Fabricate(:user, email: 'test@fundbase.com')
    first_user.valid?.should be_true # good
    first_user.save
    first_user.delete

    second_user = Fabricate(:user, email: 'test@fundbase.com')
    second_user.valid?.should be_true # good
    second_user.save

    first_user.restore
    first_user.valid?.should_not be_true # fail
  end
end

ここで、最後のテストは失敗します。何らかの理由で、電子メール フィールドの一意性がチェックされません。復元時に検証を強制する方法に関するヒントはありますか?

4

1 に答える 1

0

これはパラノイア モジュールのバグである可能性があります (これは、Mongoid の次のメジャー バージョンで実際に消える予定です: http://mongoid.org/en/mongoid/docs/extras.html#paranoia )。ユーザー アカウントを「復活」させる必要がある場合は、「有効」などの独自のブール値フィールドを追加して、false に設定してアカウントを無効にすることができます。

于 2013-06-10T12:18:33.940 に答える