我有以下模型关联
class WarehouseVariant < AB
has_one :warehouse
has_one :variant
has_many :variant_cities
end
class VariantCity < AB
belongs_to :warehouse_variant
delegate :mrp_paise, :to => :warehouse_variant
validates :variant_id, uniqueness: { scope: :city_id }
end
规范如下:
let!(:warehouse_variant) {create(:warehouse_variant) }
subject { create(:variant_city, max_discount_percent: 99.99, max_promotion_percent: 99.99, warehouse_variant: warehouse_variant) }
it { is_expected.to validate_uniqueness_of(:variant_id).scoped_to(:city_id)}
规范失败,出现以下错误:
模块:: DelegationError: VariantCity#mrp_paise委托给warehouse_variant.mrp_paise,但是warehouse_variant为nil:#
问题是我插入了一个像这样的调试语句
it {binding.pry;预计 validate_uniqueness_of(:variant_id).scoped_to(:city_id)}
这里的variant_city有对象warehouse_variant,但是当测试结束时我得到了上面提到的错误。
任何帮助都将不胜感激。
编辑:
validates_presence_of(:variant_id)传递!!