如何在没有引用的MongoDb中存储ObjectID?

时间:2012-06-04 16:15:04

标签: ruby-on-rails mongoid

我有三种模式:

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.

好的,没错。但是我如何存储,谁写了评论?

1 个答案:

答案 0 :(得分:2)

评论中的belongs_to用户正在抛弃它。只需使用普通字段来存储外键。

class Comment
  include Mongoid::Document

  embedded_in :post

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