【发布时间】:2017-03-22 09:10:19
【问题描述】:
我有一个多态模型command,它可以关联到cover 或additional_return。
这些是command 中的字段:
create_table "commands", force: :cascade do |t|
t.date "placement_date"
t.date "estimated_delivery_date"
t.string "commandable_type"
t.integer "commandable_id"
...
end
问题是:对于cover,我想验证estimated_delivery_date,但不是模型additional_return。如何只为一个模型指定验证?
class Command < ApplicationRecord
belongs_to :user, dependent: :destroy
belongs_to :commandable, polymorphic: true
validates_presence_of :commandable_type,
:commandable_id
end
【问题讨论】:
-
你可以试试
validates : estimated_delivery_date, presence: true, if: Proc.new { |c| c.commandable_type == 'Cover' }吗?
标签: ruby-on-rails ruby ruby-on-rails-5 polymorphic-associations