1

无法弄清楚为什么这不起作用

class User
  include Mongoid::Document

class Student < User
  include Mongoid::Document
  ....
  has_one :parent , class_name: "Parent", inverse_of: :children

class Parent < User
  include Mongoid::Document
  ....
  has_many :children, class_name: "Student", inverse_of: :parent

当我尝试通过设置父/子关系时

jane = Student.create!(name: "Jane")
janesParent = Parent.new(name: "Jenny")
janesParent.children.push(jane)
janesParent.save!

我收到这个错误

  When adding a(n) Student to Parent#children, Mongoid could not determine the    
  inverse foreign key to set. The attempted key was 'parent_id'.

我做错了什么?

PS我不想嵌入这些想要存储id的如果适用的类型。

4

2 回答 2

3

如果是 1-N 关系,则将Student模型关系更改为

belongs_to :parent, class_name: "Parent", inverse_of: :children
于 2013-07-29T06:01:54.557 回答
2

你试过和has_and_belongs_to_many关系吗?

于 2013-07-29T05:45:15.607 回答