0

I have a class called JobOffer that belongs to a Source. Like this:

 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

I want to, depending on the type of the Source make some validations before save, to avoid duplicate fields. In this specific case, I want that, if:

self.source.is_a? WeWorkRemotelySource

Validates if the title AND the email are the same. If the title and email are the same we will NOT SAVE this entity. This is the rule of uniquiness for this specific case.

How can I do this?

Fernando Maymone
  • 665
  • 1
  • 8
  • 17

0 Answers0