I have the following factory defined for a model:
factory :page do
association :user, factory: :standard_user
association :post, factory: [:short_post]
after :create do |model|
model.post.user = model.user
model.save!
end
end
the after create block seems to run file and the factory returns the model object with the correct/new changes however this does not persist in my test database.
ie. if i call Page.last.post.user.id
from within my test, i still get the old user id which was assigned to the post object factory earlier before the after create block. I'm not sure why this happens.
Your after :create
callback should really be updating the post
object, however, you're calling save!
on your page
object.
Try calling model.post.save!
to save the user_id
column on post
directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With