前提
ここに質問の内容を詳しく書いてください。
(例)
Ruby on Railsで楽曲を投稿する機能を作成しています。
最高音(max_sound)と最低音(min_sound)のモデルを作成し、最高音と曲(Post)、最低音と曲は関連づけられています。
曲を投稿する際にエラーが出てしまいます。
曲のモデルである、post.rbに
ruby
1 belongs_to :max_sound 2 belongs_to :min_sound
の記述を消すと問題なく投稿できるので、max_soundとpost、min_soundとpostが上手く関連付けられていないことが原因だと考えます。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- 曲を投稿したい
- 最低音と曲、最高音と曲を関連付けたい(foreign_key)
発生している問題・エラーメッセージ
Unable to autoload constant MaxSound, expected "music_app/app/models/max_sound.rb" to define it
該当のソースコード
ruby
1#post.rb 2 belongs_to :max_sound 3 belongs_to :min_sound
ruby
1#posts_controller.rb 2class PostsController < ApplicationController 3 4 def new 5 @post=Post.new 6 end 7 8 def create 9 @post=Post.new(post_params) 10 if @post.save 11 flash[:success] = "新規投稿しました" 12 redirect_to root_url 13 else 14 flash[:danger]="投稿できませんでしたもう一度投稿してください" 15 render 'new' 16 end 17 end 18 19 private 20 def post_params 21 params.require(:post).permit(:title,:singer,:picture,:max_sound_id,:min_sound_id,:category_id) 22 end 23 24end
ruby
1#min_sound.rb 2class Min_sound < ApplicationRecord 3 validates :name ,presence: true 4 validates :frequency ,presence:true 5end
ruby
1#max_sound.rb 2class Max_sound < ApplicationRecord 3 validates :name ,presence: true 4 validates :frequency ,presence:true 5end
ruby
1<div class="post-form-wrapper"> 2 <%= form_for @post do |f| %> 3 <div class="post-form"> 4 <div class="post-content"> 5 <%= f.label :max_sound_id ,"最高音を選択" %> 6 <%= f.collection_select :max_sound_id, Max_sound.all, :id, :name %> 7 <% @post.errors.full_messages_for(:max_sound_id).each do |message| %> 8 ※<%= message %> 9 <% end %> 10 </div> 11 </div> 12 13 <div class="post-form"> 14 <div class="post-content"> 15 <%= f.label :min_sound_id ,"最低音を選択" %> 16 <%= f.collection_select :min_sound_id, Min_sound.all, :id, :name %> 17 <% @post.errors.full_messages_for(:min_sound_id).each do |message| %> 18 ※<%= message %> 19 <% end %> 20 </div> 21 </div> 22 23 <%= f.submit "投稿する" %> 24 <% end %> 25</div>
試したこと
外部キーをターミナルから.schemaで確認したのですが、関連付けられているのかどこで確認したら良いのかがわかりませんでした。
terminal
1CREATE TABLE IF NOT EXISTS "max_sounds" ("id" integer NOT NULL PRIMARY KEY, "name" varchar DEFAULT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "frequency" float DEFAULT NULL); 2CREATE TABLE IF NOT EXISTS "min_sounds" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "frequency" float, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL); 3CREATE TABLE IF NOT EXISTS "posts" ("id" integer NOT NULL PRIMARY KEY, "title" varchar DEFAULT NULL, "category_id" integer DEFAULT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "picture" varchar DEFAULT NULL, "singer" varchar DEFAULT NULL, "max_sound_id" integer, "min_sound_id" integer); 4CREATE INDEX "index_posts_on_category_id" ON "posts" ("category_id"); 5CREATE INDEX "index_posts_on_user_id_and_created_at" ON "posts" ("created_at"); 6CREATE INDEX "index_posts_on_category_id_and_created_at" ON "posts" ("category_id", "created_at"); 7CREATE INDEX "index_posts_on_created_at" ON "posts" ("created_at"); 8CREATE INDEX "index_posts_on_max_sound_id" ON "posts" ("max_sound_id"); 9CREATE INDEX "index_posts_on_min_sound_id" ON "posts" ("min_sound_id");
db/schema.rbはこちらになっております。
ruby
1ActiveRecord::Schema.define(version: 20221101054623) do create_table "max_sounds", force: :cascade do |t| 2 t.string "name" 3 t.datetime "created_at", null: false 4 t.datetime "updated_at", null: false 5 t.float "frequency" 6 end 7 8 create_table "min_sounds", force: :cascade do |t| 9 t.string "name" 10 t.float "frequency" 11 t.datetime "created_at", null: false 12 t.datetime "updated_at", null: false 13 end 14 create_table "posts", force: :cascade do |t| 15 t.string "title" 16 t.integer "category_id" 17 t.datetime "created_at", null: false 18 t.datetime "updated_at", null: false 19 t.string "picture" 20 t.string "singer" 21 t.integer "max_sound_id" 22 t.integer "min_sound_id" 23 t.index ["category_id", "created_at"], name: "index_posts_on_category_id_and_created_at" 24 t.index ["category_id"], name: "index_posts_on_category_id" 25 t.index ["created_at"], name: "index_posts_on_created_at" 26 t.index ["created_at"], name: "index_posts_on_user_id_and_created_at" 27 t.index ["max_sound_id"], name: "index_posts_on_max_sound_id" 28 t.index ["min_sound_id"], name: "index_posts_on_min_sound_id" 29 end
補足情報
Rails 5.1.6
回答1件
あなたの回答
tips
プレビュー