Home > Blockchain >  Set start_time to end_time if blank
Set start_time to end_time if blank

Time:10-03

I am working on an Events Model with start datetime & end datetime. It should fill in the start datetime as the end datetime if start is blank, as stated in the test. I set up validations to require start/end presence to be true but this is obviously not what the test is asking for as it fails. I am pretty new to this and not familiar with creating/fixing tests so the answer may be right in front of me.

CodePudding user response:

sounds as if you need a before_save callback.

class Event < ApplicationRecord
  before_save :ensure_start_time

  def ensure_start_time
    start_time ||= end_time
    # i.e. set start_time = end_time if start_time is nil
  end
end
  • Related