我有一个叫做JobOffer的类,它属于一个Source。像这样:
class JobOffer < ApplicationRecord
include SoftDeletable
belongs_to :source
validates :email, uniqueness: true
validates :title, presence: true, if: :unique_from_source?
#for each kind of source, we will have uniquiness validation
def unique_from_source?
if self.source.is_a? WeWorkRemotelySource
we_work_remotely_uniquiness
end
end
def we_work_remotely_uniquiness
end
end
我想根据源的类型在保存之前进行一些验证,以避免重复的字段。在这种特定情况下,我希望这样做,如果:
self.source.is_a? WeWorkRemotelySource
验证标题和电子邮件是否相同。如果标题和电子邮件相同,我们将不会保存该实体。对于此特定情况,这是唯一性规则。
我该怎么做?