我有一些使用MongoDB,Mongoid mapper和devise的rails应用程序。 Аuthorized用户可以创建,编辑,删除帖子(脚手架)并评论此帖子。我采取了Ryan Bates截屏的评论模型示例,238集“Mongoid”。
comment.rb
class Comment
include Mongoid::Document
field :name
field :content
embedded_in :post, :inverse_of => :comments
end
post.rb
class Post
include Mongoid::Document
field :name
field :content
validates_presence_of :name
embeds_many :comments
end
user.rb
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。 我的错误在哪里?
答案 0 :(得分:2)
由于评论嵌入在帖子中,您应该有用户引用帖子。尝试删除用户中的has_many :comments
。