# If number of records = zero, display 'no records found'

**URL:** <https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320>\
**Category:** Rails\
**Created:** [July 13, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320 "2007-07-13T12:14:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [July 13, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320/1 "2007-07-13T12:14:35Z")

</div>

[I’ve no idea how this got into the ‘test’ section so sorry for the duplicate!]

I have a list of scripts, that when one is clicked on displays a new  
page with relating runs for that script.

I would like to include an if/else statement so that when the number of  
runs for a script is zero, the resulting page displays ‘no runs found’.

The runs display uses a \<% for run in @runs %\> loop to display a rows of  
run details, immediately after which I have placed \<% if (@runs.size ==  
0) %\> - however, the ‘else’ part of the statement is still run even when  
no rows are found.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [July 13, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320/2 "2007-07-13T13:39:13Z")

</div>

On Jul 13, 2007, at 6:14 AM, Paul N. wrote:

> run details, immediately after which I have placed \<% if  
> (@runs.size ==  
> 0) %\> - however, the ‘else’ part of the statement is still run even  
> when  
> no rows are found.
> 
> What am I doing wrong?

Well, for starters, not showing enough of your code, but I’ll take a  
shot anyway.

\<% if @runs.blank? -%\>

No rows found

\<% else -%\> \<% for run in @runs -%\> \<%= row\_for(run) %\> \<% end -%\> \<% end -%\>

Since .blank? works with nil, it avoids possible issues with nil.size  
== 0 (which is better expressed as @runs.empty? anyway).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [July 25, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320/3 "2007-07-25T15:33:13Z")

</div>

I’ve tried the “.blank?” addition and rails just seems to ignore it for  
some reason.

Are the minuses in the -%\> imperative btw? I’ve not seen them before.

Either way, it doesn’t seem to be working so can you suggest a way to  
debug this to see where i’m going wrong?

---

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [July 25, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320/4 "2007-07-25T18:00:18Z")

</div>

Paul N. wrote:

> I’ve tried the “.blank?” addition and rails just seems to ignore it for  
> some reason.
> 
> Are the minuses in the -%\> imperative btw? I’ve not seen them before.
> 
> Either way, it doesn’t seem to be working so can you suggest a way to  
> debug this to see where i’m going wrong?

Your ‘if’ needs to be outside the ‘for loop’. Otherwise there is nothing  
to iterate and the if statement is never hit.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [July 25, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320/5 "2007-07-25T18:01:21Z")

</div>

Jean N. wrote:

> Paul N. wrote:
> 
> > I’ve tried the “.blank?” addition and rails just seems to ignore it for  
> > some reason.
> > 
> > Are the minuses in the -%\> imperative btw? I’ve not seen them before.
> > 
> > Either way, it doesn’t seem to be working so can you suggest a way to  
> > debug this to see where i’m going wrong?
> 
> Your ‘if’ needs to be outside the ‘for loop’. Otherwise there is nothing  
> to iterate and the if statement is never hit.
> 
> Hope this helps.

OH, and \<%= -%\>, the - part means to NOT put a linefeed after the value  
being displayed.

---

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320/6 "2007-07-26T00:45:47Z")

</div>

On 7/25/07, Paul N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Either way, it doesn’t seem to be working so can you suggest a way to  
> debug this to see where i’m going wrong?

Post your code. Otherwise, everyone’s just guessing.

---

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [July 26, 2007, 10:26am UTC](https://www.ruby-forum.com/t/if-number-of-records-zero-display-no-records-found/109320/7 "2007-07-26T10:26:28Z")

</div>

Bob S. wrote:

> On 7/25/07, Paul N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Either way, it doesn’t seem to be working so can you suggest a way to  
> > debug this to see where i’m going wrong?
> 
> Post your code. Otherwise, everyone’s just guessing.

Jean’s first post identified the problem - thanks Jean 🙂
