Ruby on Rails:如何为具有两个(多个)外键的表编写种子数据



>我有四个表:

Honors (student_id,course_id,prof_id)#referencing 3 tables
student(student_id, name,phnumber,)
course(course_id, cousename)
Prof_id(prof_id,prfEmail)

为了荣誉.db

class Honor < ActiveRecord::Base
      belongs_to :course
      belongs_to :student
      belongs_to :professor
end

当然.db

class Course < ActiveRecord::Base
    has_many :honors
end

与student.rb类似,教授.rb

你能帮我为这个表写种子数据吗?

对于编写单个外键引用,我知道怎么写。 但我不确定如何在单个表上对 3 个外部引用进行操作。

单个外键示例(一个人有很多评论):

Person.create(first_name: 'Nicol',last_name: 'Cagase',dob: Date.new(1964,1,7),
            reviews: Review.create([
                {stars: 4 , title: 'The Game of Thrones', commentary: 'waiting for next episode'},
                {stars: 3 , title: 'KingsMan', commentary: 'average'},
    ]))

honor = Honors.create() 将创建您的第一个种子

honor.student.create() 将创建学生和关系

honor.course.create() 与学生相同,对教授重复

最新更新