# If variable is not empty

**URL:** <https://www.ruby-forum.com/t/if-variable-is-not-empty/196400>\
**Category:** Rails\
**Created:** [October 12, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400 "2010-10-12T19:53:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/1 "2010-10-12T19:53:55Z")

</div>

(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.

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/2 "2010-10-12T20:02:03Z")

</div>

You can try this

```
 Notifier.appointment_booked(@appointment).deliver unless 

```

@appointment.client.email.nil?

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/3 "2010-10-12T20:12:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/4 "2010-10-12T20:18:02Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/5 "2010-10-12T20:48:38Z")

</div>

On 12 October 2010 19:18, Luis S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://api.rubyonrails.org/classes/Object.html#method-i-blank)

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 8:56pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/6 "2010-10-12T20:56:26Z")

</div>

Michael P. wrote in post #949603:

> On 12 October 2010 19:18, Luis S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?
> 
> [Object](http://api.rubyonrails.org/classes/Object.html#method-i-blank)

True! Worked too 🙂

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 8:22pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/7 "2010-10-12T20:22:51Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/8 "2010-10-12T22:11:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 10:36pm UTC](https://www.ruby-forum.com/t/if-variable-is-not-empty/196400/9 "2010-10-12T22:36:27Z")

</div>

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
