Reputation: 1104
Can't work out why this is not working
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
When I try to setup the parent/child relationship via
jane = Student.create!(name: "Jane")
janesParent = Parent.new(name: "Jenny")
janesParent.children.push(jane)
janesParent.save!
I get this error
When adding a(n) Student to Parent#children, Mongoid could not determine the
inverse foreign key to set. The attempted key was 'parent_id'.
What have I done wrong?
P.S I don't want to embed these want to store the id's if applicable types.
Upvotes: 1
Views: 989
Reputation: 29094
If it is 1-N relation, change Student
model relation to
belongs_to :parent, class_name: "Parent", inverse_of: :children
Upvotes: 3
Reputation: 17631
Have you tried and has_and_belongs_to_many
relation ?
Upvotes: 2