I am trying to build factories for relatively complex models.
I have a Pressroom model that belongs to Source, and Source has many press centers. When creating a source, if the press rooms are empty, an initial press center is created in the after_create filter. The site of the press center should be unique for each source.
class Source has_many :pressrooms after_create :create_initial_pressroom! # if pressrooms.empty? ... end class Pressroom belongs_to :source # source.pressrooms.map(&:site) should have unique elements validate_on_create :check_unique_site end
This leads to my problem: the My Pressroom.make fails because it creates a source that does not have a press center, so the callback after_create creates it, and when Pressroom.make trying to complete his site is not unique. I do not want to create two press centers when I launch Pressroom.make
My attempt to solve this problem is to make the original association in the press center a link to the press center. Type Source.create :pressrooms => [Pressroom.new] .
Pressroom.blueprint do source { Source.make :pressrooms => [self] } site { source.site } end
Unfortunately, self is not yet a Press Center. This is an instance of Machinist :: Lathe, so I get an ActiveRecord :: AssociationTypeMismatch exception.
I am a little new when it comes to factories and engine drivers. I do not want to change the business logic, and I want me to be able to create press rooms with Pressroom.make without creating two press centers. If switching to factory -girl would help, I would open up to it.
I would be grateful for any ideas on how to solve this problem.
source share