# Little Live search problem

**URL:** <https://www.ruby-forum.com/t/little-live-search-problem/62726>\
**Category:** Rails\
**Created:** [June 7, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/little-live-search-problem/62726 "2006-06-07T17:23:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 7, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/little-live-search-problem/62726/1 "2006-06-07T17:23:10Z")

</div>

Works fine but when you use backspace or delete on the search word and  
it hits 0 characters it displays ALL the Test names in the database!

def live\_search  
@phrase = request.raw\_post || request.query\_string  
a1 = “%”  
a2 = “%”  
@searchphrase = a1 + @phrase + a2  
@results = Test.find(:all, :conditions =\> [ “test\_name LIKE ?”,  
@searchphrase])

```
@number_match = @results

render(:layout => false)

```

end

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 7, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/little-live-search-problem/62726/2 "2006-06-07T18:47:14Z")

</div>

Rob B. wrote:

> Works fine but when you use backspace or delete on the search word and  
> it hits 0 characters it displays ALL the Test names in the database!
> 
> def live\_search  
> @phrase = request.raw\_post || request.query\_string  
> a1 = “%”  
> a2 = “%”  
> @searchphrase = a1 + @phrase + a2  
> @results = Test.find(:all, :conditions =\> [ “test\_name LIKE ?”,  
> @searchphrase])
> 
> ```
> @number_match = @results
> 
> render(:layout => false)
> 
> ```
> 
> end

@results = @phrase.empty? ? nil : Test.find(:all, :conditions =\>  
[“test\_name LIKE ?”, @searchphrase])

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 7, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/little-live-search-problem/62726/3 "2006-06-07T19:07:11Z")

</div>

Alex W. wrote:

> @results = @phrase.empty? ? nil : Test.find(:all, :conditions =\>  
> [“test\_name LIKE ?”, @searchphrase])

Thanks Alex but I have .empty? in my view for the results.  
Any way around this?

NoMethodError in Search#live\_search

Showing app/views/search/live\_search.rhtml where line #1 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.empty?

Extracted source (around line #1):

1: \<% if @results.empty? %\>  
2: No match for ‘\<%=h @phrase %\>.’  
3: \<% else %\>  
4: \<% @results.each do |result| %\>

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 8, 2006, 12:16am UTC](https://www.ruby-forum.com/t/little-live-search-problem/62726/4 "2006-06-08T00:16:16Z")

</div>

Justin Mecham wrote:

> Try @phrase.blank? instead, as it will check for a nil value before  
> attempting to call empty?.
> 
> J

Didn’t work, same result as above.

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 8, 2006, 12:19am UTC](https://www.ruby-forum.com/t/little-live-search-problem/62726/5 "2006-06-08T00:19:43Z")

</div>

Hang on just swapped the view to say .blank? too and it works. Thanks J  
and Alex.

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 7, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/little-live-search-problem/62726/6 "2006-06-07T19:23:03Z")

</div>

Try @phrase.blank? instead, as it will check for a nil value before  
attempting to call empty?.

J
