【问题标题】:Mongoid relations with deviseMongoid 与设计的关系
【发布时间】:2011-12-01 17:21:49
【问题描述】:

我有一些带有 MongoDB、Mongoid 映射器和设计的 Rails 应用程序。 А 授权用户可以创建、编辑、删除帖子(脚手架)和评论此帖子。我以 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

用户.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。 我的错误在哪里?

【问题讨论】:

  • 您的问题需要一个更好的标题——以问题的形式。因为它是模糊的。
  • 为什么不推出自己的身份验证?在 Rails 3.1 中它非常简单。观看此 railscast:railscasts.com/episodes/270-authentication-in-rails-3-1 如果您绝对需要设计,我可以看一下,但很有可能自己做会更好/更简单。
  • @Tyler:设计非常全面。它经过了很好的测试,并且已经存在了一段时间。考虑到所涉及的时间,您必须为自己推出一个非常有力的论据。
  • jcollum,不错哈哈,但如果他不使用任何高级功能,他现在已经完成了实现。
  • 感谢泰勒的推荐。我会尝试理解设计并决定我是否需要(设计)。

标签: ruby-on-rails-3 mongodb devise mongoid


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多