【rails c】KeyError: Factory not registered: "○○"エラー→bin/spring stopコマンドで解決
エラーが起きた状況
- Rails6.0.0を使用。
- モデルのテストコードを行うため、FactoryBotというダミーデータ作成のためのgemを利用。
- 必要なgemのインストール、フォルダやファイルの作成後、ちゃんとダミーデータが作られるか確認しようと、ターミナルコマンドにおいて「rails c(console)」を実行したところ、エラー発生。
- 念のため、FactoryBotを確認するも、記述誤りは無し。
- 考えられる原因として、昨日VSコードのバージョンアップあり。
エラー内容
# コンソールを立ち上げるコマンドを実行
f.〇〇@〇〇noMacBook-Air chat-app % rails c
Running via Spring preloader in process 63751
Loading development environment (Rails 6.0.3.3)
# 今回確認したかったユーザーモデルが作成されるか実行
[1] pry(main)> FactoryBot.create(:user)
KeyError: Factory not registered: "user"
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
Caused by KeyError: key not found: "user"
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
# 他のルームモデルもエラーになるか実行
[2] pry(main)> FactoryBot.create(:room)
KeyError: Factory not registered: "room"
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
Caused by KeyError: key not found: "room"
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
解決方法
# 一回、抜ける
[3] pry(main)> exit
# コンソールを実行すると動いている「スプリング」を止める
f.〇〇@〇〇noMacBook-Air chat-app % bin/spring stop
Spring stopped.
# 止まっているかステータスを確認する
f.〇〇@〇〇noMacBook-Air chat-app % bin/spring status
Spring is not running.
# もう一度、コンソールを立ち上げる
f.〇〇@〇〇noMacBook-Air chat-app % rails c
Running via Spring preloader in process 64116
Loading development environment (Rails 6.0.3.3)
# 再度、確認したかったモデルを入力 → ダミーができた!!!
[1] pry(main)> FactoryBot.create(:user)
(0.6ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
(0.2ms) BEGIN
User Exists? (3.9ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY '[email protected]' LIMIT 1
User Create (0.5ms) INSERT INTO `users` (`name`, `email`, `encrypted_password`, `created_at`, `updated_at`) VALUES ('木村', '[email protected]', '$2a$12$vk2T9O8uxroggw9vLTSqVOd6J5CVlkOI1018qCLc7jv9rAnyfgYc.', '2020-10-05 01:38:08.395976', '2020-10-05 01:38:08.395976')
(2.0ms) COMMIT
=> #<User id: 4, name: "木村", email: "[email protected]", created_at: "2020-10-05 01:38:08", updated_at: "2020-10-05 01:38:08">
まとめ
- Railsに標準で導入されている「spring」というGemがバックグラウンドで作動していて、稀にロードエラーを起こす.
- 解決方法としては、一度作動を停止する。springの再起動は、railsコマンドと同じタイミングで行われる。
# コンソールを立ち上げるコマンドを実行
f.〇〇@〇〇noMacBook-Air chat-app % rails c
Running via Spring preloader in process 63751
Loading development environment (Rails 6.0.3.3)
# 今回確認したかったユーザーモデルが作成されるか実行
[1] pry(main)> FactoryBot.create(:user)
KeyError: Factory not registered: "user"
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
Caused by KeyError: key not found: "user"
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
# 他のルームモデルもエラーになるか実行
[2] pry(main)> FactoryBot.create(:room)
KeyError: Factory not registered: "room"
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
Caused by KeyError: key not found: "room"
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
解決方法
# 一回、抜ける
[3] pry(main)> exit
# コンソールを実行すると動いている「スプリング」を止める
f.〇〇@〇〇noMacBook-Air chat-app % bin/spring stop
Spring stopped.
# 止まっているかステータスを確認する
f.〇〇@〇〇noMacBook-Air chat-app % bin/spring status
Spring is not running.
# もう一度、コンソールを立ち上げる
f.〇〇@〇〇noMacBook-Air chat-app % rails c
Running via Spring preloader in process 64116
Loading development environment (Rails 6.0.3.3)
# 再度、確認したかったモデルを入力 → ダミーができた!!!
[1] pry(main)> FactoryBot.create(:user)
(0.6ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
(0.2ms) BEGIN
User Exists? (3.9ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY '[email protected]' LIMIT 1
User Create (0.5ms) INSERT INTO `users` (`name`, `email`, `encrypted_password`, `created_at`, `updated_at`) VALUES ('木村', '[email protected]', '$2a$12$vk2T9O8uxroggw9vLTSqVOd6J5CVlkOI1018qCLc7jv9rAnyfgYc.', '2020-10-05 01:38:08.395976', '2020-10-05 01:38:08.395976')
(2.0ms) COMMIT
=> #<User id: 4, name: "木村", email: "[email protected]", created_at: "2020-10-05 01:38:08", updated_at: "2020-10-05 01:38:08">
まとめ
- Railsに標準で導入されている「spring」というGemがバックグラウンドで作動していて、稀にロードエラーを起こす.
- 解決方法としては、一度作動を停止する。springの再起動は、railsコマンドと同じタイミングで行われる。
# 一回、抜ける
[3] pry(main)> exit
# コンソールを実行すると動いている「スプリング」を止める
f.〇〇@〇〇noMacBook-Air chat-app % bin/spring stop
Spring stopped.
# 止まっているかステータスを確認する
f.〇〇@〇〇noMacBook-Air chat-app % bin/spring status
Spring is not running.
# もう一度、コンソールを立ち上げる
f.〇〇@〇〇noMacBook-Air chat-app % rails c
Running via Spring preloader in process 64116
Loading development environment (Rails 6.0.3.3)
# 再度、確認したかったモデルを入力 → ダミーができた!!!
[1] pry(main)> FactoryBot.create(:user)
(0.6ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
(0.2ms) BEGIN
User Exists? (3.9ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY '[email protected]' LIMIT 1
User Create (0.5ms) INSERT INTO `users` (`name`, `email`, `encrypted_password`, `created_at`, `updated_at`) VALUES ('木村', '[email protected]', '$2a$12$vk2T9O8uxroggw9vLTSqVOd6J5CVlkOI1018qCLc7jv9rAnyfgYc.', '2020-10-05 01:38:08.395976', '2020-10-05 01:38:08.395976')
(2.0ms) COMMIT
=> #<User id: 4, name: "木村", email: "[email protected]", created_at: "2020-10-05 01:38:08", updated_at: "2020-10-05 01:38:08">
- Railsに標準で導入されている「spring」というGemがバックグラウンドで作動していて、稀にロードエラーを起こす.
- 解決方法としては、一度作動を停止する。springの再起動は、railsコマンドと同じタイミングで行われる。
Author And Source
この問題について(【rails c】KeyError: Factory not registered: "○○"エラー→bin/spring stopコマンドで解決), 我々は、より多くの情報をここで見つけました https://qiita.com/cherry_2020/items/0801da656ecfb9ee991e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .