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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Q&A

解決済

1回答

2138閲覧

rails mysqlのエラーについて ActiveRecord::StatementInvalid

rrtts667

総合スコア18

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

0グッド

1クリップ

投稿2020/07/30 10:34

編集2020/07/30 12:26

railsでお気に入り機能を下記のサイトを見てやっている最中です。
https://qiita.com/qz7_start/items/2430feca6df9ccf87257

それを行なったが以下のエラーが出ました。
ActiveRecord::StatementInvalid in Tweets#show

Mysql2::Error: Unknown column 'favorites.tweet_id' in 'where clause'
イメージ説明

html

1<% if tweet.favorites.where(user_id: current_user.id).exists? %> 2<%= link_to "お気に入りから削除", tweet_favorites_path(tweet_id: tweet.id), method: :delete, remote: true %> 3<% else %> 4<%= link_to "お気に入りに追加", tweet_favorites_path(tweet_id: tweet.id), method: :post, remote: true %> 5<% end %>

favorite.rb(お気に入り)

rails

1class FavoritesController < ApplicationController 2 def index 3 @favorites = current_user.favorites 4 end 5 6 def create 7 @product = Product.find(params[:product_id]) 8 favorite = current_user.favorites.new(product_id: @product.id) 9 favorite.save 10 end 11 12 def destroy 13 @product = Product.find(params[:product_id]) 14 @favorites = current_user.favorites 15 favorite = current_user.favorites.find_by(product_id: @product.id) 16 favorite.destroy 17 end 18end 19

ご教授願えたら幸いです。
よろしくお願いします。

追加

スキーマー

rails

1ctiveRecord::Schema.define(version: 2020_07_14_100939) do 2 3 create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 4 t.integer "user_id" 5 t.integer "tweet_id" 6 t.text "text" 7 t.datetime "created_at", precision: 6, null: false 8 t.datetime "updated_at", precision: 6, null: false 9 end 10 11 create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 12 t.string "title" 13 t.string "text" 14 t.string "image" 15 t.datetime "created_at", precision: 6, null: false 16 t.datetime "updated_at", precision: 6, null: false 17 t.integer "user_id" 18 end 19 20 create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 21 t.string "email", default: "", null: false 22 t.string "encrypted_password", default: "", null: false 23 t.string "reset_password_token" 24 t.datetime "reset_password_sent_at" 25 t.datetime "remember_created_at" 26 t.datetime "created_at", precision: 6, null: false 27 t.datetime "updated_at", precision: 6, null: false 28 t.string "nickname" 29 t.index ["email"], name: "index_users_on_email", unique: true 30 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 31 end 32 33 create_table "favorites", force: :cascade do |t| 34 t.integer "user_id", null: false 35 t.integer "tweet_id", null: false 36 t.datetime "created_at", null: false 37 t.datetime "updated_at", null: false 38 end 39 40end 41

favoriteモデル

rails

1class Favorite < ApplicationRecord 2 belongs_to :user 3 belongs_to :tweet 4end 5

こちらになります。

関連した記事のスキーマーの記載の箇所に

rails

1schema.rb 2 create_table "users", force: :cascade do |t| 3(略) 4 end 5 create_table "products", force: :cascade do |t| 6(略) 7 end 8 9 create_table "favorites", force: :cascade do |t| 10 t.integer "user_id", null: false 11 t.integer "product_id", null: false 12 t.datetime "created_at", null: false 13 t.datetime "updated_at", null: false 14 end

とありますがもしかしたら

rails

1 create_table "users", force: :cascade do |t| 2(略) 3 end 4 create_table "products", force: :cascade do |t| 5(略) 6 end

も入れた方がよろしいでしょうか?

追加
rails migrateを行なった時に
以下のようになります。

rails

1ActiveRecord::Schema.define(version: 2020_07_30_062001) do 2 3 create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 4 t.integer "user_id" 5 t.integer "tweet_id" 6 t.text "text" 7 t.datetime "created_at", precision: 6, null: false 8 t.datetime "updated_at", precision: 6, null: false 9 end 10 11 create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 12 t.datetime "created_at", precision: 6, null: false 13 t.datetime "updated_at", precision: 6, null: false 14 end 15 16 create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 17 t.string "title" 18 t.string "text" 19 t.string "image" 20 t.datetime "created_at", precision: 6, null: false 21 t.datetime "updated_at", precision: 6, null: false 22 t.integer "user_id" 23 end 24 25 create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 26 t.string "email", default: "", null: false 27 t.string "encrypted_password", default: "", null: false 28 t.string "reset_password_token" 29 t.datetime "reset_password_sent_at" 30 t.datetime "remember_created_at" 31 t.datetime "created_at", precision: 6, null: false 32 t.datetime "updated_at", precision: 6, null: false 33 t.string "nickname" 34 t.index ["email"], name: "index_users_on_email", unique: true 35 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 36 end 37 38end 39

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

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

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

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

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

winterboum

2020/07/30 11:46

model Favorite のスキーマと関連定義を見せてください
rrtts667

2020/07/30 12:20

ご対応ありがとうございます。 質問を修正しています。 ご確認よろしくお願いします。
guest

回答1

0

自己解決

schemaにて保存が出来ない為
マイグレーションファイルにて
favorites

rails

1 t.integer "user_id", null: false 2 t.integer "tweet_id", null: false

こちらを追加し
rails db:rollback
を行い
rails db:migrate
を行うとschemaにて保存確認が出来た。

投稿2020/07/31 06:49

rrtts667

総合スコア18

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問