2012-06-04 39 views
1

我有三個型號:如何在沒有引用的情況下在MongoDb中存儲ObjectID?

class User 
    include Mongoid::Document 
    field :name, :type => String 

    has_many :comments 
    embeds_many :posts 
end 

class Post 
    include Mongoid::Document 
    field :title, :type => String 
    field :body, :type => String 

    embeds_many :comments 
    belongs_to :user 
end 

class Comment 
    include Mongoid::Document 
    field :text, :type => String 

    belongs_to :user 
    embedded_in :post 
end 

而且我有這樣的錯誤:

Referencing a(n) Comment document from the User document via a relational association is not allowed since the Comment is embedded. 

好了,這就是正確的。但我如何儲存,誰寫了評論?

回答

2

評論中的belongs_to用戶是什麼在拋棄它。只需使用普通字段來存儲外鍵。

class Comment 
    include Mongoid::Document 

    embedded_in :post 

    field :text, :type => String 
    field :commenter_id 
end 
+0

ty!這比我想象的要容易:) – enRai

相關問題