Skip check after creating mangoid rails

I want to skip checking after creating the object. Take an example

a person has many companies, and a company has many people.

a person has many placements and placements belongs to a person a person can have only one active placement

There is one check in the placement model that checks if a person has an active place when saving.

@placement is active placement
@employment.placement = @person

if @placement.save
  #################
  @person.placements << @placement
  @company.placements << @placement
end

Now that the placement is saved for the first time, there is no problem saving it.

Now the problem arises when

@person.placements << @placement

Because the person already has active placement via @ placement.save.

@ person.placements <<@placement again saves @placement and validation corrects the validation error of the @placement object.

Is there any way that I do not miss this particular check, where I am in the ############# area of ​​my code.

.

+5
2

: save: validate = > false

+11

, , -

if @placement.valid?
  @person.placements << @placement
  @company.placements << @placement
end

, mongoid, << .save .

<< mongoid, .

validates :placeholder, :on => :create Or

if @placement.valid?
  @placement.person = @person
  @company.placements << @placement
end
0

All Articles