mardi 22 mai 2018

Model embeds_many and embedded_in relationship using factorygirls

I want to write FactoryGirl class for creating company. The models are as follows:

module Company
  class Contact
    include Mongoid::Document
    include ActiveModel::Validations

    embedded_in :company, class_name: "::Company::Contact"

  end
end

module Company
  class Company

    require 'autoinc'

    embeds_many :contacts, class_name: "::Company::Contact"

  end
end


FactoryGirl.define do
  factory :company, :class => 'Company::Company' do
    name { Faker::Company.name }

    after(:create) do |company|
      # company.contacts << create(:company_contact)
      create_list(:company_contact, 1, company: company)
    end
    # contacts { [ build(:company_contact) ] }
  end
end

The error received is

Failure/Error: create_list(:company_contact, 1, company: company)

 Mongoid::Errors::InvalidPath:

   message:
     Having a root path assigned for Company::Contact is invalid.
   summary:
     Mongoid has two different path objects for determining the location of a document in the database, Root and Embedded. This error is raised when an embedded document somehow gets a root path assigned.
   resolution:
     Most likely your embedded model, Company::Contact is also referenced via a has_many from a root document in another collection. Double check the relation definitions and fix any instances where embedded documents are improperly referenced from other collections

How should I handle this? I cannot change the models.

Aucun commentaire:

Enregistrer un commentaire