7

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.

L457
  • 1,002
  • 1
  • 13
  • 33

1 Answers1

4

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.

Anthony E
  • 11,072
  • 2
  • 24
  • 44