質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

解決済

1回答

1167閲覧

migrateができない [NameError]

takoyaki2355

総合スコア11

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2022/02/08 11:32

実現したいこと
参考書を見ながらオリジナルアプリ開発途中、テーブル内に不備があったためモデルとマイグレーションファイルを削除してから作り直すことにしました。

やったこと
マイグレーションファイルのステータスを確認してダウン、削除
モデルもdocker-compose run web rails destroy model userで削除
その後

docker-compose run web rails g model user schoolname:string email:string address:string telephonenumber:string password_digest:string

上記コードでモデルとマイグレーションファイル作成

モデル class User < ApplicationRecord has_secure_password validates :schoolname, presence: true, uniqueness: true validates :email, presence: true, uniqueness: true validates :address, presence: true, uniqueness: true validates :telephonenumber, presence: true, uniqueness: true has_many :tweets, dependent: :destroy end
マイグレーションファイル class CreateUsers < ActiveRecord::Migration[6.1] def change create_table :users do |t| t.string :schoolname, null: false t.string :email, null: false t.string :address, null: false t.string :telephonenumber, null: false t.string :password_digest, null: false t.timestamps t.index :schoolname, email, address, telephonenumber, unique: true end end end

作成後、上記の内容に編集してマイグレートを実行すると以下のエラーが出てきます。

undefined local variable or method `email' for #<CreateUsers:0x0000555f6cf200c8 @name="CreateUsers", @version=20220208091654, @connection=nil> NameError: undefined local variable or method `email' for #<CreateUsers:0x0000555f6cf200c8 @name="CreateUsers", @version=20220208091654, @connection=nil>

2つのエラー文について色々調べてみましたがよく分からず、email未定義となっていますが具体的にどうしたら良いのかも分かりません。
どなたかアドバイスお願いします。

開発環境
dockerファイル

From ruby:3.0.1 RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \ && apt-get update -qq \ && apt-get install -y nodejs yarn mariadb-client WORKDIR /app COPY ./src /app RUN bundle config --local set path 'vendor/bundle' \ && bundle install

docker-compose.yml

version: '3.0' services: db: image: mysql:8.0.27 command: --default-authentication-plugin=mysql_native_password volumes: - ./src/db/mysql_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: password web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - ./src:/app ports: - "3000:3000" depends_on: - db

gemfile

source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '3.0.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.4', '>= 6.1.4.1' # Use mysql as the database for Active Record gem 'mysql2', '~> 0.5' # Use Puma as the app server gem 'puma', '~> 5.0' # Use SCSS for stylesheets gem 'sass-rails', '>= 6' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 5.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password gem 'bcrypt', '~> 3.1.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.4', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 4.1.0' # Display performance information such as SQL time and flame graphs for each request in your browser. # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md gem 'rack-mini-profiler', '~> 2.0' gem 'listen', '~> 3.3' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 3.26' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'slim-rails' gem 'html2slim' gem 'bootstrap' gem 'ransack' gem 'kaminari'

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

email未定義となっていますが具体的にどうしたら良いのかも分かりません。

ruby

1t.index :schoolname, email, address, telephonenumber, unique: true

t.indexは、(複合インデックスを作るのでなければ)1列ずつしか書けませんし、email以降がシンボルではないです(エラーはこれが原因と思われます)。

投稿2022/02/08 11:56

maisumakun

総合スコア145184

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takoyaki2355

2022/02/08 12:14

ありがとうございます! 1発で直りました! 数日間悩んでたのがバカみたいです笑 本当に助かりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問