虽然它在开发和生产中都有效,但一旦我尝试在 RSpec 或 Cucumber 中利用 has_many :through 关系,它就会返回 nil。像这样:
订单模型(简化):
class Order < ActiveRecord::Base
has_one :shipping_address, through :checkout
end
结帐模型(简化):
class Checkout < ActiveRecord::Base
has_one :shipping_address, :class_name => 'ShippingAddress', :as => :addressable
end
规格:
describe "shipping_address" do
it "should return a ShippingAddress" do
@order.checkout.shipping_address.is_a? ShippingAddress # return true
@order.shipping_address.is_a? ShippingAddress # returns false (is NilClass)
end
end