我的模型Product
包含字段:
name price product_type_id:integer size:string page_quantity:integer.
如何使此验证适用于Product
模型create
操作?
- if product_type.id == "1"
validates :size, presence: true
validates :page_quantity, presence:false
- else
validates :size, presence: false
validates :page_quantity, presence:true
end
答案 0 :(得分:2)
validates :size, presence: true, if: Proc.new { |p| p.product_type_id == "1" }
validates :page_quantity, presence:false, if: Proc.new { |p| p.product_type_id == "1" }
validates :size, presence: false, unless: Proc.new { |p| p.product_type_id == "1" }
validates :page_quantity, presence:true, unless: Proc.new { |p| p.product_type_id == "1" }
end