Factory Girl、Mongoid 和嵌入式文档

发布于 2024-11-07 06:25:48 字数 1806 浏览 1 评论 0原文

我正在使用 Mongoid 和 Devise 设置我的第一个应用程序。我正在尝试使用此工厂在我的测试中工厂化用户:

Factory.define(:user) do |f|
  f.email "[email protected]"
  f.password "testing"
  f.password_confirmation "testing"
end

当我尝试工厂化此工厂时,我收到此错误:

NoMethodError: undefined method `add_on_blank' for []:Array
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criteria.rb:213:in `send'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criteria.rb:213:in `method_missing'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/relations/embedded/many.rb:373:in `send'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/relations/embedded/many.rb:373:in `method_missing'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/named_scope.rb:121:in `with_scope'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/relations/embedded/many.rb:372:in `send'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/relations/embedded/many.rb:372:in `method_missing'

我的模型如下所示:

用户:

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

  attr_accessible :email, :password, :password_confirmation, :remember_me

  embeds_many :errors
end

错误:

class Error
  include Mongoid::Document
  field :klass, :type => String
  embedded_in :user
end

如何在不同时创建任何嵌入文档的情况下创建用户模型?

I am setting up my first app with Mongoid and Devise. I am trying to Factory a user in my test with this factory:

Factory.define(:user) do |f|
  f.email "[email protected]"
  f.password "testing"
  f.password_confirmation "testing"
end

When I try and factory this I get this error:

NoMethodError: undefined method `add_on_blank' for []:Array
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criteria.rb:213:in `send'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criteria.rb:213:in `method_missing'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/relations/embedded/many.rb:373:in `send'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/relations/embedded/many.rb:373:in `method_missing'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/named_scope.rb:121:in `with_scope'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/relations/embedded/many.rb:372:in `send'
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/relations/embedded/many.rb:372:in `method_missing'

My models look like:

User:

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

  attr_accessible :email, :password, :password_confirmation, :remember_me

  embeds_many :errors
end

Error:

class Error
  include Mongoid::Document
  field :klass, :type => String
  embedded_in :user
end

How should a user model be created without creating any embedded documents at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文