I have an an object Search
with a method listings
that returns an array of hashes if there are search results, otherwise it returns an empty array. In the event there is any empty array, I need to skip some code and go straight to the show page. I've tried object.nil?
object.empty?
object.present?
all with the same outcome....the object which is supposed to be nil is treated as non-nil.
Controller code:
def show
@search = Search.find(params[:id])
@results = @search.listings
if @results.last.present?
if @results.last[0] == "p" || @results.last[0] == "s" || @results.last[0] == "d"
p "@results.pop is either p, s, or d"
@sort_column = @results.pop
@grade = @sort_column.gsub(/[^0-9,.]/, "") unless @results.last[0] == "d"
end
end
end
show action results in
undefined method `[]' for nil:NilClass
Extracted source (around line #21):
19 p "@results.pop is either p, s, or d"
20 @sort_column = @results.pop
21 @grade = @sort_column.gsub(/[^0-9,.]/, "") unless @results.last[0] == "d"
22 end
23 end
24 end
but, the server interface verifies that @results.last is nil:
>> @results
=> []
>> @results.last
=> nil
>> @results.last.present?
=> false
>> @results.last[0]
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/tomb/Projects/schoolsparrow/app/controllers/searches_controller.rb:21:in `show'
>>
I'm at a loss as to how to logic is getting past the results.last.present?
when results.last
is nil
.