Hello,
Is it possible to use an association as the value for another association in a trait?
Let me describe a (simplified) example. I have 3 models
- User
- Forum (belongs_to :forum)
- Thread (belongs_to :forum, belongs_to :user)
Please note that Thread contains an user_id, it does not belong to User using Forum (at the time being, I can't change such constraint).
What I'd like to know, is if there a way to define a trait like the one
FactoryGirl.define do
factory :thread do
trait :with_forum do
association :user
# Lazy-evaluate the value of user to the previously created factory
association :forum, user: user
end
end
end
What the trait is supposed to do is to create an user and associate it to the thread.
Then it should create a forum, but the user should be the same instance previously created.
There are mainly two reasons:
- The user associated with the forum is the same of the thread
- I don't want to create two user factories on cascade
Any idea? I tried to use the lazy evaluation, but no way to use it with associations.
Thanks,
-- Simone