You can use callbacks in factory. Here is an example of it :
FactoryGirl.define do
factory :coupon do
code { rand(25**25) }
percent_discount { rand(100**1) }
start_at { Time.now }
end_at { 30.day.from_now }
after(:create) do |c|
#do job related stuff
end
end
end
Checkout its documentation for more information :
https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#callbacks
UPDATE
Based on your last comment, I believe trait will not be useful. Here is something I understand create coupon > do some process > execute coupon > assign job
. So after creating coupon I think there is some delay / process logic of coupon. So when you execute coupon at this time you need to create object of job and associate with that coupon. I believe following would be suitable for that flow :
FactoryGirl.define do
factory :coupon do
code { rand(25**25) }
percent_discount { rand(100**1) }
start_at { Time.now }
end_at { 30.day.from_now }
#This trait is used in case,
#if you want job to be assigned to coupon
#for example create(:coupon,:executed)
trait :executed do |c|
association :job, factory: [:job]
c.executed_at { Time.now }
end
end
end
#In Test Case
@coupon = create(:coupon)
#have some test for coupon before execution
#now executing coupon
@coupon.update_attributes(job_id: create(:job), executed_at: Time.now)