2013-07-29 64 views
1

想不通這是爲什麼不工作Mongoid自基準集的has_many不工作

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'. 

我做錯了什麼?

P.S我不想嵌入這些想要存儲的ID如果適用的類型。

回答

2

如果是1-N的關係,改變Student模型關係

belongs_to :parent, class_name: "Parent", inverse_of: :children 
1

您是否試過與has_and_belongs_to_many的關係?

+0

燁的作品。然而,我想塑造一個父母有很多孩子,一個孩子只有一個父母。即1 .... N關係 – dboyd68