1

我有三个模型:

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.

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

4

1 回答 1

2

评论中的belongs_to用户是什么把它扔掉了。只需使用普通字段来存储外键。

class Comment
  include Mongoid::Document

  embedded_in :post

  field :text, :type => String
  field :commenter_id
end
于 2012-06-04T18:08:10.837 回答