0
我有1 ...在Mongoid意外行爲Mongoid
class Student < User
include Mongoid::Document
...
has_many :courses
class Course
include Mongoid::Document
belongs_to :student
N的關係設置我有2名學生
student1 = Student.create!(...)
student2 = Student.create!(...)
course1 = Course.create!(name: "Course 1")
course2 = Course.create!(name: "Course 2")
course3 = Course.create!(name: "Course 3")
student1.courses = [course1,course2]
student1.save!
student2.courses = [course2,course3]
student2.save!
理想的情況是我想要在MongoDB中
Student{
coures:[CourseId,CourseId,CourseId]
}
我在兩條戰線上並不理想
a)關係保存在Coures中(我知道這是根據mongoid文檔想知道我是否可以翻轉這個嗎?)
b)課程只保存一個學生id而不是一個集合。所以我輸了數據\
例如在蒙戈
Courses
{
"_id" : ObjectId("51f607bdb5b6057c92000005"),
"name" : "Course 1",
"student_id" : ObjectId("51f607bdb5b6057c92000002"),
}
{
"_id" : ObjectId("51f607bdb5b6057c92000006"),
"name" : "Course 2",
"student_id" : ObjectId("51f607bdb5b6057c92000003"),
}
{
"name" : "Course 3",
"student_id" : ObjectId("51f607bdb5b6057c92000003"),
}
我明白http://mongoid.org/en/mongoid/docs/relations.html#has_many 顯示,這是正確的。
但這不是我所期望的。我正在嘗試做什麼有一些關係?