【发布时间】:2019-01-01 06:29:49
【问题描述】:
我有一个具有如下关联的模型:-
class Order < ApplicationRecord
belongs_to :customer, inverse_of: :orders
belongs_to :user, inverse_of: :orders
belongs_to :shipping_address, class_name: "Customer::Address", inverse_of: :shipped_orders
belongs_to :billing_address, class_name: "Customer::Address", inverse_of: :billed_orders
end
另外,customer_address 有一个字段 customer_id。我的工厂是这样的:-
FactoryGirl.define do
factory :order do
customer
user
association :shipping_address, factory: :customer_address, customer_id: customer.id
association :billing_address, factory: :customer_address, customer_id: customer.id
end
end
但我无法访问 customer.id。我收到此错误:-
undefined method `id' for #<FactoryGirl::Declaration::Implicit:0x007fa3e6979d70>
如何将 customer.id 传递给 shipping_address 和 billing_address 关联?
【问题讨论】:
标签: ruby-on-rails ruby testing ruby-on-rails-5 factory