I have the following factories:
factory :post do
title "lorem"
content "lorem ipsum"
category_id
user
end
factory :category do
name "Lorem"
end
I want to fill category_id somehow with the :category factory.
Pretty much mimicking what I do in my view:
<%= f.collection_select :category_id, Category.order(:name), :id, :name,
{ prompt: 'Select Category' } %>
(The categories in this case are already created).
How to accomplish that?
If you just replace
category_idbycategoryin yourpostfactory, like this:then factory_girl will initiate the association using the
categoryfactory, in the process settingcategory_idon thePostrecord. For details see the documentation on factory_girl interaction with ActiveRecord.