How can I create an if statement to check if a variable contains a
value? If it does contain a value, I want to send an email. I’m doing
the following but it doesn’t work…
respond_to do |format|
if @appointment.save
# send email only if a client email is set
if defined?(appointment.client.email)
Notifier.appointment_booked(@appointment).deliver
end
Alex S. wrote in post #949576:
Looks like you’re checking whether or not the variable itself exists, as
opposed to if it contains a value.
Try this instead:
if appointment.client.email #do stuff & things
end
In this instance, if ‘email == nil’, the statement will evaluate to
false.
appointment belongs to client. so the appointments table has a client_id
column. and the clients table has an email column.
I think the variable its supposed to be set and empty. I tried your
suggestion…
def create @appointment = Appointment.new(params[:appointment])
respond_to do |format|
if @appointment.save
# send email only if a client email is set
if @appointment.client.email
Notifier.appointment_booked(@appointment).deliver
end
But I see the logs and it seems that it tries to send an email even if
there is no email for the client.