2
0

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.

Doorkeeper を 使っていて ユーザーの削除があったときに、関連するデータベースのエントリを削除する

Last updated at Posted at 2017-01-11

Doorkeeper : Doorkeeper is an OAuth 2 provider for Rails.
https://github.com/doorkeeper-gem/doorkeeper
OAuth2 対応 APIを自前で用意するときに使えるえらいgemです。

さて、これを使うとき、resource_owner と 自分たちが持つアプリケーションのユーザーと紐付けるわけですが、紐付いているため、ユーザーのdestroyをしようとすると、DBでエラーを吐きます。
なので、関連する、DBエントリを一緒に削除すると考えることにしましょう。

user.rb
class User < ApplicationRecord
  # Relationship
  has_many :oauth_access_grants, class_name: 'Doorkeeper::AccessGrant', foreign_key: 'resource_owner_id', :dependent => :destroy
  has_many :oauth_access_tokens, class_name: 'Doorkeeper::AccessToken', foreign_key: 'resource_owner_id', :dependent => :destroy
  
end
2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?