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

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

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

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

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

2回答

279閲覧

RubyonRails 多対多 アソシエーション

mikan0777

総合スコア10

Ruby on Rails

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

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2020/03/02 11:21

前提・実現したいこと

<エラーが出るまでの背景>
〇1人の生徒は多数のイベントに応募する
〇1つのイベントは多数の生徒から応募される
という関係性をDBのテーブルで表すために

Student applies(中間テーブル用のモデル) Event

この3つのモデルを定義しモデルファイルで
多対多のアソシエーションを定義し、
マイグレーションファイルを実行し、
viewで一人の生徒が応募したeventを
全て表示しようとしたところ、
エラーが出てしまいました。
(studentはdeviseを使ってモデル定義しました)

<開発環境>
awsのcloud9でrubyonrailsを利用してweleveという名前のwebアプリケーションを開発しています。DBはMySQLを利用しています

発生している問題・エラーメッセージ

NameError in Students#show Showing /home/ec2-user/environment/weleve/app/views/students/show.html.erb where line #12 raised: uninitialized constant Student::StudentEvent Extracted source (around line #12): <参加するイベント> 12 <%= @student.events.each do |event| %> 13 </div> 14 <h2><%= event.title %></h2> 15 <ul>

event.rb

class Event < ApplicationRecord has_many :applies has_many :events, through: :applies belongs_to :group accepts_nested_attributes_for :applies end

student.rb

student.rb

1class Student < ApplicationRecord 2 3 has_one_attached :image 4 attr_accessor :grade 5 has_one_attached :image 6 7 # Include default devise modules. Others available are: 8 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 9 devise :database_authenticatable, :registerable, 10 :recoverable, :rememberable, :validatable 11 12 13 has_many :applies 14 has_many :events, through: :applies 15 16 17 has_many :connections 18 has_many :groups, through: :connections 19 20 accepts_nested_attributes_for :applies 21end

apply.rb

class Apply < ApplicationRecord belongs_to :events belongs_to :student end

studentsのマイグレーションファイル

# frozen_string_literal: true class DeviseCreateStudents < ActiveRecord::Migration[5.2] def change create_table :students do |t| ## Database authenticatable t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" t.integer :grade ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable # t.integer :sign_in_count, default: 0, null: false # t.datetime :current_sign_in_at # t.datetime :last_sign_in_at # t.string :current_sign_in_ip # t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps null: false end add_index :students, :email, unique: true add_index :students, :reset_password_token, unique: true # add_index :students, :confirmation_token, unique: true # add_index :students, :unlock_token, unique: true end end

appliesのマイグレーションファイル

class CreateApplies < ActiveRecord::Migration[5.2] def change create_table :applies do |t| t.references :student, index: true, foreign_key: true t.references :event, index: true, foreign_key: true t.bigint :student_id t.bigint :event_id t.timestamps end end end

eventsのマイグレーションファイル

class CreateEvents < ActiveRecord::Migration[5.2] def change create_table :events do |t| t.string :title t.date :date t.string :descrip t.string :where t.integer :group_id t.timestamps end end end

試したこと

〇多対多のアソシエーションの組み方が間違っていないか確認

〇中間テーブルのマイグレーションファイルにreferenceがきちんと定義されているか

補足情報(FW/ツールのバージョンなど)

いろいろ調べてみたのですが自力では全く解決できそうにないので質問させていただきました。どなたがご教授いただけると幸いです。

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

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

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

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

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

guest

回答2

0

ベストアンサー

apply.rb の

belongs_to :events

belongs_to :event
にしてみてください。
ちょっと気になるのがあるので、それでうまく行かなかったら、エラーメッセージとともに db/schema.rb を載せてください。

投稿2020/03/02 11:35

winterboum

総合スコア23401

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

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

mikan0777

2020/03/02 12:30

上手くいきました!ありがとうございます!!!!
guest

0

参考情報

  • Active Record の関連付け

https://railsguides.jp/association_basics.html

  • 【初心者向け】丁寧すぎるRails『アソシエーション』チュートリアル

https://qiita.com/kazukimatsumoto/items/14bdff681ec5ddac26d1

投稿2020/03/02 13:19

katoy

総合スコア22324

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問