0

I have a factory that looks like:

factory :order_with_approval do
   status      'approved'
   association :approval, factory: :approval
end

Now, I need the approval model to take the order id and set it as the eventable id. I've been able to figure this out in the other direction by doing:

factory :cancellation do
  reason 'a reason'
  cancellation_date Date.current
  association :eventable, factory: :order
end

So what might work is if I can pass in something other than a new factory into the association. What I'd like to do is pass in the parent factory. Is that possible? Can I achieve this some other way?

1 Answer 1

0

Cross-references might be resolved with after clause as shown here:

factory :order_with_approval do
  status 'approved'
  association :approval, factory: :approval

  after(:build, :create) do |order, _evaluator|
    order.approval.update_attributes!(eventable: order)
  end
end
4
  • This would work to solve the problem listed above, but I'm running into an issue where this is being done in a call back upon creation. So, the creation blows up before it gets the approval created.
    – Noah Clark
    Sep 12, 2018 at 18:16
  • Then just assign the value instead of calling update_attributes. Sep 13, 2018 at 4:23
  • It never gets to that step because the association blows up. I think there is a better way to structure the factories that I'm playing around with.
    – Noah Clark
    Sep 13, 2018 at 12:51
  • Create the approval association inside the after callback. Sep 13, 2018 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.