我有一些带有 MongoDB、Mongoid 映射器和设计的 Rails 应用程序。 Аauthorized 用户可以创建、编辑、删除帖子(脚手架)和评论该帖子。我以 Ryan Bates 截屏视频的评论模型示例为例,238 集“Mongoid”。

评论.rb

class Comment
  include Mongoid::Document
  field :name
  field :content
  embedded_in :post, :inverse_of => :comments
end

后.rb
class Post
      include Mongoid::Document
      field :name
      field :content
      validates_presence_of :name
      embeds_many :comments
    end

用户名
class User
  include Mongoid::Document
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable

  field :username

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_many :comments
  references_many :post

end

但是当我尝试注册新用户时,在注册表单中推送“注册”,我看到这个错误
Mongoid::Errors::MixedRelations in Devise::RegistrationsController#create

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

我用Mysql db开始这个应用程序,然后决定进入mongo。
我的错误在哪里?

最佳答案

由于评论嵌入在帖子中,您应该让用户引用帖子。尝试删除用户中的 has_many :comments

关于ruby-on-rails-3 - 与设计的蒙古人关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8345546/

10-12 03:42