これは Order クラスにあります。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.