前提・実現したいこと
stock_itemsテーブルとtagsテーブル(多対多)をデータベースにreference型で外部キーを設定したい
発生している問題・エラーメッセージ
down 20210804105711 Create stock items down 20210804111228 Create tags down 20210824112715 Create item connects watanabekenta@GV9395 n-web_app % rails db:migrate == 20210804105711 CreateStockItems: migrating ================================= -- create_table(:stock_items) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'n_web_app_development.tags' doesn't exist /Users/watanabekenta/orijinals/n-web_app/db/migrate/20210804105711_create_stock_items.rb:3:in `change' /Users/watanabekenta/orijinals/n-web_app/bin/rails:9:in `<top (required)>' /Users/watanabekenta/orijinals/n-web_app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' エラーメッセージ
該当のソースコード
stockitem
1class CreateStockItems < ActiveRecord::Migration[6.0] 2 def change 3 create_table :stock_items do |t| 4 t.string :stock_item_manufacturer, null: false 5 t.string :stock_item_name, null: false 6 t.string :stock_item_standard, null: false 7 t.string :stock_item_strage_condition, null: false 8 t.text :stock_item_description, null: false 9 t.references :tag, foreign_key: true 10 t.timestamps 11 end 12 end 13end
tag
1class CreateTags < ActiveRecord::Migration[6.0] 2 def change 3 create_table :tags do |t| 4 t.string :tag_word, null: false, uniqueness: true 5 t.references :stock_item, foreign_key: true 6 t.timestamps 7 end 8 end 9end
stockitemrb
1class StockItem < ApplicationRecord 2 3 has_one_attached :image 4 has_many :item_connects, dependent: :destroy 5 has_many :tags, through: :item_connects 6 7 extend ActiveHash::Associations::ActiveRecordExtensions 8 belongs_to :genre 9 10end 11
tagrb
1class Tag < ApplicationRecord 2 3 has_many :item_connects 4 has_many :stock_items, through: :item_connects 5 6end
itemconnectrb
1class ItemConnect < ApplicationRecord 2 3 belongs_to :stock_item 4 belongs_to :tag 5end 6
stockitemcontroller
1class StockItemsController < ApplicationController 2 3 before_action :search_stock_item, only: [:index, :search] 4 5 def index 6 @stock_items = StockItem.all.order(created_at: :desc) 7 end 8 9 def new 10 @stock_item = ItemsTag.new 11 end 12 13 def create 14 @stock_item = ItemsTag.new(stock_item_params) 15 if @stock_item.save 16 return redirect_to root_path 17 else 18 render "new" 19 end 20 end 21 22 def edit 23 @stock_item = StockItem.find(params[:id]) 24 end 25 26 def update 27 if @stock_item.update(stock_item_params) 28 redirect_to contacts_path 29 else 30 render :edit 31 end 32 end 33 34 def destroy 35 @stock_item = StockItem.find(params[:id]) 36 if @stock_item.destroy 37 redirect_to root_path 38 else 39 render :index 40 end 41 end 42 43 def search 44 @results = @p.result.includes(:tag) 45 end 46 47 private 48 49 def stock_item_params 50 params.require(:items_tag).permit(:stock_item_manufacturer, :stock_item_name, :stock_item_standard, :stock_item_strage_condition, :stock_item_description, :image, :tag_word) 51 end 52 53 def search_stock_item 54 @p = StockItem.ransack(params[:q]) 55 end 56 57end
試したこと
rails db:migrate:reset
アソシエーションの確認しましたがおかしな部分が見つかりません。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/28 13:37
2021/09/28 22:42
2021/10/01 12:18
2021/10/08 12:20