我的订单课上有这个。我想验证 的存在,shipping_address
除非它与billing_address
. 然而,我的规格不断失败。
class Order < ActiveRecord::Base
attr_writer :ship_to_billing_address
belongs_to :billing_address, class_name: 'Address'
belongs_to :shipping_address, class_name: 'Address'
accepts_nested_attributes_for :billing_address, :shipping_address
validates :shipping_address, presence: true, unless: -> { self.ship_to_billing_address? }
def ship_to_billing_address
@ship_to_billing_address ||= true
end
def ship_to_billing_address?
self.ship_to_billing_address
end
end
但我不断收到失败的规格(预期的例子无效):
describe "shipping_address_id" do
context "when shipping address is different from billing address" do
before { @order.ship_to_billing_address = false }
it_behaves_like 'a foreign key', :shipping_address_id
end
end
shared_examples 'a foreign key' do |key|
it "can't be nil, blank, or not an int" do
[nil, "", " ", "a", 1.1].each do |value|
@order.send("#{key}=", value)
@order.should_not be_valid
end
end
end
表格代码:
= f.check_box :ship_to_billing_address
| Use my shipping address as my billing address.