I have four models: User
, Product
, Ownership
and Location
. User
and Product
have one Location
, and Location
belongs to User
and to Product
(Polymorphic model).
I want to use FactoryGirl
to create products that have the same location as their owner.
factory :location do
sequence(:address) { |n| "#{n}, street, city" }
end
factory :user do
sequence(:name) { |n| "Robot #{n}" }
sequence(:email) { |n| "numero#{n}@robots.com"}
association :location, factory: :location
end
factory :product do
sequence(:name) { |n| "Objet #{n}" }
association :location, factory: :location
end
factory :ownership do
association :user, factory: :user
association :product, factory: :product
end
I created a method in the product model file to retrieve the product's owner just by doing product.owner
.
I want to adapt the product factory in order to replace the factoried location by product.owner.location
. How can I do that?
EDIT 1
I want to use it like that:
First I create a user
FactoryGirl.create(:user)
Later I create a product
FactoryGirl.create(:product)
When I associate both of them
FactoryGirl.create(:current_ownership, product: product, user: user)
I want that the location of my product becomes the one of his owner.