Blankman Blankman - 2 months ago 6
Ruby Question

How to set a property once it has been saved and re-save a child property?

factory :user do
company
phone "123-123-1234"
.
.

end


The company model has a property that I need to set also:

company.owner_id = -1


But I need to set the owner_id to the user's id.

How can I do this?

Answer

You can use callback

factory :user do
  company
  phone "123-123-1234"
  .
  .
  after(:create) { |user| user.company.update(owner_id: user.id) }
end