Reputation: 19969
I have a bit of an unusual situation with a tool that I'm not that familiar with. I have a factory called favorite_location and I need two variables from it, the global_id and now the id (as the location_id below). I'm not trying to do anything fancy - just really need this value:
factory :favorite_location, class: GlobalList do
global_id { FactoryGirl.create(:location).global_id } # would work
list_id { FactoryGirl.create(:user).liked_restaurant_list.id }
location_id { FactoryGirl.create(:location).id } #will fail with duplicate location
end
Is there any way to do the following? I should mention that this is a denormalized relationship.
factory :favorite_location, class: GlobalList do
{ location = FactoryGirl.create(:location) }
global_id { location.global_id }
list_id { FactoryGirl.create(:user).liked_restaurant_list.id }
location_id { location.id } #will fail with duplicate location
end
thx for any help
Upvotes: 1
Views: 951
Reputation: 4545
I think you probably want to use a transient attribute, to ensure you get a new location with each different favorite_location instance - see https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#transient-attributes
Something like
factory :favorite_location, class: GlobalList do
ignore do
location_for_vars { create(:location) }
end
global_id { location_for_vars.global_id }
list_id { FactoryGirl.create(:user).liked_restaurant_list.id }
location_id { location_for_vars.id }
end
Upvotes: 3