Engineer, how can I refer to the object that I am doing and pass it to the association? (AssociationTypeMismatch)

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.

+4
source share
2 answers

Going to the search, I found some tips on http://webcrisps.wordpress.com/2009/08/13/stubbing-before_create-callbacks-in-a-machinist-blueprint/ - to mute the after_create :create_initial_pressroom! on the source, in the original plan - using Machinist 2 and mocha here:

 Pressroom.blueprint do source { Source.make!(:without_initial_pressroom) } site { object.source.site } end Source.blueprint do site end Source.blueprint(:without_initial_pressroom) do object.stubs(:create_initial_pressroom!).returns(true) end 

Thus, Pressroom.make! works as it should, Source.make! It works as it should, and ... I guess I'm happy. But still a little puzzled by the problems that I ran into in the solution I tried above (both on drivers 1 and 2).

If anyone knows how to make this work with object let me know. That would be a lot cleaner, and besides, I generally don't like accepting my own answers here on stackoverflow.

+2
source

Are you using driver 1 or 2? These suggestions are for driver 2 and may or may not work in driver 1. I don’t remember how you do it in Machinist 1 (and can’t be bothered by google!).

To do so, you have to offer, you need to use object :

 Pressroom.blueprint do source { Source.make :pressrooms => [object] } site { source.site } end 

But a much better way to do this is to take advantage of the fact that Machinst knows about model associations and just lets him do his job:

 Pressroom.blueprint do source site { source.site } end 

Assuming your associations are set up correctly, this should work. See the Blueprints widget page for more details.

+1
source

Source: https://habr.com/ru/post/1345122/


All Articles