LoginSignup
0
0

More than 3 years have passed since last update.

destroyを実行した際に発生した「uninitialized constant User::Post」の解決策

Last updated at Posted at 2021-03-28

エラー内容

userを削除する処理(destroyアクション)を実行した際に発生

NameError in Users::RegistrationsController#destroy
uninitialized constant User::Post
Extracted source (around line #237):
235
236
237
238
239
240

            end

            raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
          end
        end

原因

uninitialized constant User::Post

上記を翻訳すると
「初期化されていない定数User :: Post」

UserモデルとPostモデルの関連付けを行った際に、
User.rbに「has_many :posts, dependent: :destroy 」を記述し、
Userのデータを削除した際に、関連付けられているPostモデルのデータも一緒に削除するように設定していた。

その後、Postモデル自体を削除。

その為、Postモデルが存在しないのに削除しようとしていたため、「そもそもPostモデルは存在しないですよ」とエラーが発生したということです。

解決法

User.rbに記述してある「has_many :posts, dependent: :destroy 」の一文を削除。

0
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
0
0