In a simple booking app:
- When user selects a seat, a
TempAppointmentis created. - After the seat has been paid for, an Appointment is created based off the info in the TempAppointment record.
The Appointment record cannot be created first, since the passenger may not pay, in which case the TempAppointment stays as-in, and an associated Appointment record never gets created.
My natural thinking is that a TempAppointment has_one Appointment (which works), but when I add optional: true, I see errors:
class TempAppointment < ApplicationRecord
has_one :appointment, optional: true
end
try to create a new TempAppointment
ta = TempAppointment.new(cruise_id: 1, passenger_id: 1, start_time: start_time, end_time: start_time + 3600)
ArgumentError: Unknown key: :optional. Valid keys are: :class_name, :anonymous_class, :foreign_key,
:validate, :autosave, :foreign_type, :dependent, :primary_key, :inverse_of, :required, :as, :touch
Why can't has_one work with optional: true?
has_oneisoptional: trueby default (even if it isn't really an option of this method, I mean has_one never means it is require)So if you set the optional: true to the other model, be careful, this means that this other model doesn't need to be in a relationship with the first one. It's a one way dependence.
But if you do