我正在尝试使用多态方法将电子邮件嵌入到人 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