我有三个模型:用户、产品和所有权。所有权属于用户和产品。产品和用户有许多所有权。
我使用 FactoryGirl gem 创建了以下工厂:
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
我这样使用它:
let(:user) { FactoryGirl.create(:user) }
let(:product) { FactoryGirl.create(:product) }
let(:ownership) { FactoryGirl.create(:current_ownership, user: user, product: product) }
但我想改进我的工厂,为了做到这一点:
let(:user) { FactoryGirl.create(:user) }
let(:product) { FactoryGirl.create(:product, owner: user) }
你知道怎么做吗?