LoginSignup
2
4

More than 5 years have passed since last update.

Rails:「ActiveRecord::RecordNotUnique: Mysql2::Error: Duplicate entry '' for key 'index_users_on_unlock_token'」の対処方法

Last updated at Posted at 2018-09-29

発生した問題

cancancan、devise、rails_adminを使用した実装で、管理画面からUserモデルの属性の更新を行う際に

ActiveRecord::RecordNotUnique: Mysql2::Error: Duplicate entry '' for key 'index_users_on_unlock_token'」

というエラーに遭遇したため、対処方法記録として残したいとお思います。

今回参考にした、公式の文書です。
https://github.com/sferik/rails_admin/wiki/Fields#exclusion

今回参考にした記事です
https://stackoverflow.com/questions/21949125/index-users-on-unlock-token-unique-validation-error-in-rails-devise
https://github.com/starterkits/rails4-starterkit/issues/23

結論【rails_admin.rbにコードを追加】

config/initializers/rails_admin.rb
config.model 'User' do
    exclude_fields :unlock_token
end

今回の問題点

unlock_tokenがUniqueにも関わらず、""のStringが渡されていることでエラーが発生しております。
そのため、exclude_fields :unlock_token
とし、unlock_tokenを除外することでエラー回避を図りました。

ご参考になれば幸いです。

2
4
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
2
4