# How do I base and IF off of a Nil array object

**URL:** <https://www.ruby-forum.com/t/how-do-i-base-and-if-off-of-a-nil-array-object/180211>\
**Category:** Rails\
**Created:** [December 14, 2009, 9:14pm UTC](https://www.ruby-forum.com/t/how-do-i-base-and-if-off-of-a-nil-array-object/180211 "2009-12-14T21:14:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stilgar71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@stilgar71](https://www.ruby-forum.com/u/stilgar71)\
**Post date:** [December 14, 2009, 9:14pm UTC](https://www.ruby-forum.com/t/how-do-i-base-and-if-off-of-a-nil-array-object/180211/1 "2009-12-14T21:14:00Z")

</div>

First please excuse the crudeness of my code but I need to understand  
how this works.

This is code in a controller looking to output to a view.  
I have selected all the tasks from my module based on active status. In  
this case the @persontasks object is nil. I get nothing back. I want  
to look at that object with the below “if” statement and determine if I  
can use the object. The below does not work it always goes to the else  
condition.  
Any help would be much appreciated…

def show

```
# Get all tasks for that member id

@persontasks = @person.tasks.find(:all, :conditions =>["status = ?",

```

‘active’])

```
logger.debug "Look here ARIC"

if(@persontasks.nil?) (THIS DOES NOT WORK ALWAYS GOES TO ELSE)
  logger.debug "Object is Nil"
  else
  logger.debug "Object is not Nil"
  @sort_ptaks = @persontasks.sort_by{|e| e.due_on}
end
```

---

<div class="post-metadata">

**Author:** ![stilgar71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@stilgar71](https://www.ruby-forum.com/u/stilgar71)\
**Post date:** [December 14, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/how-do-i-base-and-if-off-of-a-nil-array-object/180211/2 "2009-12-14T21:24:31Z")

</div>

> Any help would be much appreciated…
> 
> def show
> 
> ```
> # Get all tasks for that member id
> 
> @persontasks = @person.tasks.find(:all, :conditions =>["status = ?",
> 
> ```
> 
> ‘active’])
> 
> ```
> logger.debug "Look here ARIC"
> 
> if(@persontasks.nil?) (THIS DOES NOT WORK ALWAYS GOES TO ELSE)
> logger.debug "Object is Nil"
> else
> logger.debug "Object is not Nil"
> @sort_ptaks = @persontasks.sort_by{|e| e.due_on}
> end
> 
> ```

Now is a good time for you to start using “script/console” (or on  
windows, \>ruby script/console).  
You’ll find out upon running it that:  
@persontasks = @person.tasks.find(:all, :conditions =\>[“status = ?”,  
‘active’]

returns : []  
Which is an empty array, which is not nil.  
Why? Good question! You are looking for :all, aren’t you? Maybe you  
meant to look for :first, which would return the first record (or, can  
you guess? That’s right! It’ll return nil if it can’t find anything).

---

<div class="post-metadata">

**Author:** ![stilgar71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@stilgar71](https://www.ruby-forum.com/u/stilgar71)\
**Post date:** [December 14, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/how-do-i-base-and-if-off-of-a-nil-array-object/180211/3 "2009-12-14T21:33:28Z")

</div>

Aldric G. wrote:

> > Any help would be much appreciated…
> > 
> > def show
> > 
> > ```
> > # Get all tasks for that member id
> > 
> > @persontasks = @person.tasks.find(:all, :conditions =>["status = ?",
> > 
> > ```
> > 
> > ‘active’])
> > 
> > ```
> > logger.debug "Look here ARIC"
> > 
> > if(@persontasks.nil?) (THIS DOES NOT WORK ALWAYS GOES TO ELSE)
> > logger.debug "Object is Nil"
> > else
> > logger.debug "Object is not Nil"
> > @sort_ptaks = @persontasks.sort_by{|e| e.due_on}
> > end
> > 
> > ```
> 
> Now is a good time for you to start using “script/console” (or on  
> windows, \>ruby script/console).  
> You’ll find out upon running it that:  
> @persontasks = @person.tasks.find(:all, :conditions =\>[“status = ?”,  
> ‘active’]
> 
> returns : []  
> Which is an empty array, which is not nil.  
> Why? Good question! You are looking for :all, aren’t you? Maybe you  
> meant to look for :first, which would return the first record (or, can  
> you guess? That’s right! It’ll return nil if it can’t find anything).

I am starting to use console and it is helping me out tremendously…  
Thanks so much!! just a couple of additional quesitons …  
How do interogate that empty array? when I try iterate and compare to  
instance of the object(elemets in the array) it blows up with this  
error: You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.\<=\>

What should my if statement look like ?

---

<div class="post-metadata">

**Author:** ![stilgar71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@stilgar71](https://www.ruby-forum.com/u/stilgar71)\
**Post date:** [December 14, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/how-do-i-base-and-if-off-of-a-nil-array-object/180211/4 "2009-12-14T21:56:20Z")

</div>

Stil Gar wrote:

> > > def show
> > > 
> > > ```
> > > # Get all tasks for that member id
> > > 
> > > @persontasks = @person.tasks.find(:all, :conditions =>["status = ?",
> > > 
> > > ```
> > > 
> > > ‘active’])
> > > 
> > > ```
> > > logger.debug "Look here ARIC"
> > > 
> > > if(@persontasks.nil?) (THIS DOES NOT WORK ALWAYS GOES TO ELSE)
> > > logger.debug "Object is Nil"
> > > else
> > > logger.debug "Object is not Nil"
> > > @sort_ptaks = @persontasks.sort_by{|e| e.due_on}
> > > end
> > > 
> > > ```
> > 
> > @persontasks = @person.tasks.find(:all, :conditions =\>[“status = ?”,  
> > ‘active’]  
> > returns : [], which is an empty array, which is not nil.

> I am starting to use console and it is helping me out tremendously…  
> Thanks so much!! just a couple of additional questions …  
> How do interrogate that empty array? when I try iterate and compare to  
> instance of the object(elements in the array) it blows up with this  
> error: You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.\<=\>
> 
> What should my if statement look like ?

if @persontasks.empty? # Is this an empty array?  
logger.debug “No tasks found for this person”  
else  
logger.debug “Tasks found! Roll up them sleeves!”  
@sort\_ptask = @persontasks.sort\_by { |e| e.due\_on }  
end

How about that?

---

<div class="post-metadata">

**Author:** ![stilgar71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@stilgar71](https://www.ruby-forum.com/u/stilgar71)\
**Post date:** [December 16, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/how-do-i-base-and-if-off-of-a-nil-array-object/180211/5 "2009-12-16T15:15:19Z")

</div>

Aldric G. wrote:

> if @persontasks.empty? # Is this an empty array?  
> logger.debug “No tasks found for this person”  
> else  
> logger.debug “Tasks found! Roll up them sleeves!”  
> @sort\_ptask = @persontasks.sort\_by { |e| e.due\_on }  
> end

While this is indeed valid code, is it really necessary at all? Wouldn’t  
it be more efficient to let the database do the sorting and then remove  
this block of code entirely?

I could be missing something, but I can’t see why it’s necessary to do  
an in-memory sort using #sort\_by in this case.

---

<div class="post-metadata">

**Author:** ![stilgar71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@stilgar71](https://www.ruby-forum.com/u/stilgar71)\
**Post date:** [December 16, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/how-do-i-base-and-if-off-of-a-nil-array-object/180211/6 "2009-12-16T15:19:43Z")

</div>

Robert W. wrote:

> Aldric G. wrote:
> 
> > if @persontasks.empty? # Is this an empty array?  
> > logger.debug “No tasks found for this person”  
> > else  
> > logger.debug “Tasks found! Roll up them sleeves!”  
> > @sort\_ptask = @persontasks.sort\_by { |e| e.due\_on }  
> > end
> 
> While this is indeed valid code, is it really necessary at all? Wouldn’t  
> it be more efficient to let the database do the sorting and then remove  
> this block of code entirely?
> 
> I could be missing something, but I can’t see why it’s necessary to do  
> an in-memory sort using #sort\_by in this case.

You’re entirely right, as far as I can tell.  
[http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002313&name=find](http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002313&name=find)

@persontasks = @person.tasks.find(:all, :conditions =\>[“status =  
?”,‘active’], :order =\> “due\_on ASC”)

This is probably the best way to go, but introducing too many concepts  
at once can be confusing 😉 I just wanted to answer precisely the  
question he had asked, which, maybe, was a mistake.
