5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

devise + paranoiaで論理削除を考慮したバリデーション

Posted at

このエントリの内容だけだとDevise側の実装でうまくいかなかったので。
http://dangerous-animal141.hatenablog.com/entry/2014/09/08/133533

gem 'paranoia_uniqueness_validator'を追加した上で
下記のバリデートを追加

  validates :email,
    :presence => true,
    :uniqueness_without_deleted => true

これだけだとNotUniqueなバリデートエラーが返る。

deviseのライブラリを覗いてみるとこうなってる。

# lib/devise/models/validatable.rb
validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed?

ので、ユニークチェックが余分にかかってしまう。
これをキャンセルして:uniqueness_without_deleted => trueだけが効くようにするために、ResouceModelに下記足してやる。

def email_changed?
  false
end

※ Rails3での挙動なので、4こんなことやんなくてもいいのかもしれない

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?