If variable is not empty

(MOVED FROM RUBY FORUM)

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…


EXTRACT

def create
@appointment = Appointment.new(params[:appointment])

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.

You can try this

 Notifier.appointment_booked(@appointment).deliver  unless 

@appointment.client.email.nil?

Luis S. wrote in post #949585:

You can try this

 Notifier.appointment_booked(@appointment).deliver  unless

@appointment.client.email.nil?

According to the logs, it’s still trying to send an email. Note “Sent
mail to” but of course, it’s empty because there is no email for that
client.


LOG TRACE

Rendered notifier/appointment_booked.html.erb (5.9ms)
Rendered notifier/appointment_booked.text.erb (0.3ms)

Sent mail to (75ms)
Date: Tue, 12 Oct 2010 13:08:51 -0500

right, I was checking for nil but that’s a string “”. Instead do this.

Notifier.appointment_booked(@appointment).deliver unless
@appointment.client.email.empty?

On 12 October 2010 19:18, Luis S. [email protected] wrote:

right, I was checking for nil but that’s a string “”. Instead do this.

Notifier.appointment_booked(@appointment).deliver unless
@appointment.client.email.empty?

Better off using the Rails method .blank? which returns true for nil,
empty strings, empty arrays, all sorts:

@appointment.client.email.blank?

http://api.rubyonrails.org/classes/Object.html#method-i-blank

Michael P. wrote in post #949603:

On 12 October 2010 19:18, Luis S. [email protected] wrote:

right, I was checking for nil but that’s a string “”. Instead do this.

Notifier.appointment_booked(@appointment).deliver unless
@appointment.client.email.empty?

Better off using the Rails method .blank? which returns true for nil,
empty strings, empty arrays, all sorts:

@appointment.client.email.blank?

http://api.rubyonrails.org/classes/Object.html#method-i-blank

True! Worked too :slight_smile:

Luis S. wrote in post #949592:

right, I was checking for nil but that’s a string “”. Instead do this.

Notifier.appointment_booked(@appointment).deliver unless
@appointment.client.email.empty?

AWESOME!!! Finally it works!

Thank you so much :smiley:

Hi, you can do the following:

unless appointment.client.email.blank?

do something

end

Next, you may want to do additional because some objects in the chain
could be nil.

Good luck,

-Conrad

Sent from my iPhone

On Oct 12, 2010, at 12:19 PM, Conrad T. wrote:

Hi, you can do the following:

unless appointment.client.email.blank?

do something

end

Next, you may want to do additional because some objects in the chain could be
nil.

try() comes in handy for this…

unless appointment.client.try(:email).blank?

-philip

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs