所以我想做这样的事情,我一直在玩语法,环顾四周,但仍然没有雪茄。这可能吗?
validates :description, presence: false unless object.attached_model.description == "Custom"
has_many: :attached_model
答案 0 :(得分:1)
validates :description, :presence => true,
:unless => Proc.new { |a| a.attached_model.description == "Custom" }
我看到明治先生也提到了这一点。您可以在指南中查看更多示例。但请注意,您无法验证:presence =>假(这没什么)。如果你想验证它是空白的,那么你需要编写一个自定义验证器。
validate :description_not_present
def description_not_present
errors.add(:description, "should be blank") if description.present? && a.attached_model.description != "Custom"
end
我在这里使用的是attachment_model(singular),因为这是你在你的例子中使用的,如果你只有has_many然后相应地改变它,就像MrYoshiji指出的那样。