2012-06-29 53 views
1

我試圖使用多態的方法在人類Mongoid對象內嵌入電子郵件。獲取「BSON :: InvalidDocument:無法將類Mongoid :: Relations :: Embedded :: Many的對象序列化爲BSON。」每當我運行測試。請參閱下面的代碼,任何將不勝感激。我不確定在FactoryGirl的個人內部建立電子郵件的正確方法。謝謝。Mongoid Polymorphic Association + FactoryGirl + RSpec

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 

回答

0

下面是我上次在mongoid中使用帶有嵌入關聯的Factory Girl的方法。相反,請在用戶工廠嘗試此操作。

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