Смоделируйте отношения embeds_many и embedded_in с помощью factorygirls - PullRequest
0 голосов
/ 22 мая 2018

Я хочу написать класс FactoryGirl для создания компании.Модели следующие:

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

Получена ошибка

Ошибка / ошибка: 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я должен справиться с этим?Я не могу изменить модели.

1 Ответ

0 голосов
/ 22 мая 2018

У вас неверное имя класса ассоциации:

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

    embedded_in :company, class_name: 'Company::Company'
  end
end
...