dimanche 27 décembre 2020

Factory_bot, how to reuse parent factory in relationship where child belongs to both parent and parents parent?

Shift belongs_to both Employee and Site while Employee belongs_to Site

Given that, here is a factory:

  factory :site do
    ...
  end
  
  factory :employee do
    site
    ...
  end

  factory :shift do
    employee # creates a site
    site # creates a new site; i.e., this is NOT same site as employee site, each call creates a new site
    ...
  end

When I run create(:shift), I'd like the default created shift.site to be the same as the shift.employee.site, but currently the behavior is that 2 sites are created, when I only want one.

Thanks,

Aucun commentaire:

Enregistrer un commentaire