ActiveRecord::RecordInvalid: Validation failed: Email can't be blank


ゲストユーザーを seed.rb に入れて、必要な情報を環境変数にしていたが、Email can't be blank というエラーがでた。

#結論

＝（全角）を使っていた。

```.env
RAILS_GUEST_ADDRESS＝[任意のアドレス]
```


#なぜ発見が遅れたのか？

言い訳をすると、見た目ではさっぱりわからなかった。

以下が、seed.rb。VSCode によって文字が彩られている。
![Screen Shot 2021-04-29 at 19.37.02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/335807/12cea402-db63-78aa-3a7f-749bbf7efc81.png)

そして、こっちが、.env。無地である。
![Screen Shot 2021-04-29 at 19.42.18.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/335807/02d04438-3b1a-73ba-1bc0-b9fdc8c43eee.png)

実際に＝（全角）を使ってしまったのだけれど、非常にわかりづらい。



#関連ファイル一覧

Gemfile

```
gem 'dotenv-rails'
```
ご存知、'dotenv-rails'。環境変数を導入するGem。

```.env
RAILS_GUEST_USER=[任意の名前]
RAILS_GUEST_ADDRESS＝[任意のアドレス]
RAILS_GUEST_KEY=[任意の文字列]
```
環境変数のファイル。上のコードには、＝（全角）を使用している。


```seed.rb
User.create!(
  name: ENV['RAILS_GUEST_USER'],
  email: ENV['RAILS_GUEST_ADDRESS'],
  password: ENV['RAILS_GUEST_KEY'],
  password_confirmation: ENV['RAILS_GUEST_KEY'],
  admin: false
)
```
最後にシード。環境変数を入れ込んでいる。


以上、自戒を込めて。

###エラー全文
```
bin/rails db:seed
rails aborted!
ActiveRecord::RecordInvalid: Validation failed: Email can't be blank
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/validations.rb:80:in `raise_validation_error'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/validations.rb:53:in `save!'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/transactions.rb:318:in `block in save!'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/transactions.rb:375:in `block in with_transaction_returning_status'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `block in transaction'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/transactions.rb:212:in `transaction'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/transactions.rb:366:in `with_transaction_returning_status'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/transactions.rb:318:in `save!'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/suppressor.rb:48:in `save!'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/persistence.rb:55:in `create!'
/app/db/seeds.rb:7:in `<main>'
/usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/usr/local/bundle/gems/railties-6.0.3.6/lib/rails/engine.rb:557:in `block in load_seed'
/usr/local/bundle/gems/railties-6.0.3.6/lib/rails/engine.rb:675:in `with_inline_jobs'
/usr/local/bundle/gems/railties-6.0.3.6/lib/rails/engine.rb:557:in `load_seed'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/tasks/database_tasks.rb:440:in `load_seed'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/railties/databases.rake:331:in `block (2 levels) in <main>'
/usr/local/bundle/gems/railties-6.0.3.6/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/usr/local/bundle/gems/railties-6.0.3.6/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/usr/local/bundle/gems/railties-6.0.3.6/lib/rails/command.rb:48:in `invoke'
/usr/local/bundle/gems/railties-6.0.3.6/lib/rails/commands.rb:18:in `<main>'
/usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
```