【问题标题】:Mongoid Polymorphic Association + FactoryGirl + RSpecMongoid 多态关联 + FactoryGirl + RSpec
【发布时间】:2012-06-29 11:22:42
【问题描述】:

我正在尝试使用多态方法将电子邮件嵌入到人 Mongoid 对象中。获取“BSON::InvalidDocument:无法将 Mongoid::Relations::Embedded::Many 类的对象序列化为 BSON。”每当我运行测试。请参阅下面的代码,任何将不胜感激。我不确定在 FactoryGirl 内部建立电子邮件的正确方法是什么。谢谢。

class Email
  include Mongoid::Document
  include Mongoid::Timestamps
  embedded_in :mailable, polymorphic: true

  field :email, type: String
  field :category, type: String
end

class Person
  include Mongoid::Document
  embeds_many :emails, as: :mailable       #polymorhpic
  index  "emails.email", unique: true

  field :first_name, type: String
  field :middle_name, type: String
  field :last_name, type: String

  validates_uniqueness_of :emails

end


FactoryGirl.define do
  sequence(:fn) {|n| "first_name#{n}" }
  sequence(:ln) {|n| "last_name#{n}" }
  factory :person do
    first_name { generate(:fn) }
    last_name { generate(:ln) }
    gender 'M'
    nationality 'USA'
    ssn '123-88-1111'
    factory :emails_ do
      emails { Factory.build(:email) }
    end
  end
end

FactoryGirl.define do
  sequence(:address) {|n| "user#{n}@mail.com" }
  factory :email do
    email { generate(:address) }
    category 'personal'
  end
end

【问题讨论】:

    标签: mongoid factory-bot


    【解决方案1】:

    这是我上次在 mongoid 中使用带有嵌入式关联的 Factory Girl 时采用的方法。请在您的用户工厂尝试此操作。

    emails { |e| [e.association(:email)] }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      相关资源
      最近更新 更多