Rails db:migrate relation does not exist

前端 未结 1 598
星月不相逢
星月不相逢 2021-01-23 01:05

I have such models:

student:

class Student < ActiveRecord::Base
  has_many :participations, dependent: :destroy
  has_many :subject_item_notes, depe         


        
1条回答
  •  醉话见心
    2021-01-23 01:34

    First, check your sequence of migrations. It seems that you are trying to create a reference before creating the table subject_items itself.

    You should first create students table then your subject_items table then add the reference. Or alternatively add the reference columns yourself when creating the tables which is all what add_reference does.

    Also, You have two relations from Student to SubjectItem.

    has_many :subject_items, dependent: :destroy, through: :participations
    has_many :subject_items, dependent: :destroy
    

    You need to your first relation in order for ActiveRecord to be able to identify the correct one first for querying then for your migration. Think of the query that will be generated if you called @student.subject_items for example. Will it go through the first relation or the second one.

    Your relations should be ex:

    has_many :participation_subject_items, dependent: :destroy, through: :participations
    has_many :subject_items, dependent: :destroy
    

    0 讨论(0)
提交回复
热议问题