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

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

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

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 ?

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?

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.

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

@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 :wink: I just wanted to answer precisely the
question he had asked, which, maybe, was a mistake.