# Activerecord and empty find results

**URL:** <https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112>\
**Category:** Rails\
**Created:** [July 23, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112 "2007-07-23T13:13:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [July 23, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112/1 "2007-07-23T13:13:04Z")

</div>

is this the prefered way to render a special rhtml-file if a  
Record.find leads to an empty result?

def dummy  
@users = User.find(:all, :conditions =\> “username = ‘dsr321’”)  
if @users.blank?  
redirect\_to :action =\> “emptyresult”  
end  
end

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [July 23, 2007, 1:35pm UTC](https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112/2 "2007-07-23T13:35:01Z")

</div>

Jochen K. wrote:

> is this the prefered way to render a special rhtml-file if a  
> Record.find leads to an empty result?
> 
> def dummy  
> @users = User.find(:all, :conditions =\> “username = ‘dsr321’”)  
> if @users.blank?  
> redirect\_to :action =\> “emptyresult”  
> end  
> end

I usually use the same template with some conditional logic to check if  
the resultset is empty. You could do what you’re suggesting if you want  
a totally different view. Or you could just choose to render another  
template (IIRC this should do it, check the API otherwise):

render :template =\> “mycontroller/emptyresult”

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [July 23, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112/3 "2007-07-23T13:41:21Z")

</div>

> > end  
> > render :template =\> “mycontroller/emptyresult”  
> > Ok, but is this the prefered syntax:

if @users.blank?

or should I use something different?

When I render the template with an empty result set I get “nil” but  
“if @users.nil?” does not work.

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [July 23, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112/4 "2007-07-23T13:42:23Z")

</div>

On 7/23/07, Jacob A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > end
> > 
> > ```
> > 
> > end
> 
> I usually use the same template with some conditional logic to check if  
> the resultset is empty. You could do what you’re suggesting if you want  
> a totally different view. Or you could just choose to render another  
> template (IIRC this should do it, check the API otherwise):
> 
> render :template =\> “mycontroller/emptyresult”

The advice from [http://therailsway.com/](http://therailsway.com/) is to avoid conditional logic  
in views with more than one branch. It’s OK to say “display x if y”  
but it’s not OK to say “if x, display y, else display z”. Based on  
that, I’d avoid having this decision in the template itself.

I’m not sure what the conventional wisdom is re: whether to redirect  
to a different action or simply render a different template, but I  
would (personally) favor rendering the different template unless there  
was additional preparation necessary which might cloud the meaning of  
the first action.

David

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [July 23, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112/5 "2007-07-23T13:45:30Z")

</div>

On 7/23/07, Jochen K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > redirect_to :action => "emptyresult"
> > > 
> > > ```
> > 
> > render :template =\> “mycontroller/emptyresult”
> 
> Ok, but is this the prefered syntax:
> 
> if @users.blank?
> 
> or should I use something different?
> 
> When I render the template with an empty result set I get “nil” but  
> “if @users.nil?” does not work.

Assuming you’re doing something like @users = User.find(…), you  
should have an empty array. If you’re doing something different please  
post it. Otherwise I’d try @users.empty?

Also - you shouldn’t be getting nil from @users. Are you explicitly  
referencing one of them in the view? Like this: @users[0]?

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [July 23, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112/6 "2007-07-23T14:39:00Z")

</div>

David C. wrote:

> > > ```
> > > end
> > > 
> > > ```
> 
> but it’s not OK to say “if x, display y, else display z”. Based on  
> that, I’d avoid having this decision in the template itself.

Move it into a helper then.

> I’m not sure what the conventional wisdom is re: whether to redirect  
> to a different action or simply render a different template, but I  
> would (personally) favor rendering the different template unless there  
> was additional preparation necessary which might cloud the meaning of  
> the first action.

I agree that render is better than redirect to avoid the extra http  
request. But note that the idea of having different templates / actions  
based on the number of results in a resultset breaks down as soon as the  
number of resultsets displayed grows due to the combinatorial explosion.

That is if you wants to display both users and posts you will have to  
create 4 templates, add another variable and this number grows to 8 -  
not to mention the logic in the controller to handle this.

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [July 23, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/activerecord-and-empty-find-results/110112/7 "2007-07-23T13:52:48Z")

</div>

> > > > ```
> > > > @users = User.find(:all, :conditions => "username =
> > > > 
> > > > ```
> > > 
> > > want  
> > > or should I use something different?
> > 
> > When I render the template with an empty result set I get “nil”  
> > but  
> > “if @users.nil?” does not work.
> 
> Assuming you’re doing something like @users = User.find(…), you  
> should have an empty array.

I use this one:

unless @users.empty?  
redirect\_to :action =\> “list”  
else  
redirect\_to :action =\> “fehler”  
end

and everything works fine.

> If you’re doing something different  
> please post it. Otherwise I’d try @users.empty?

> Also - you shouldn’t be getting nil from @users. Are you  
> explicitly referencing one of them in the view? Like  
> this: @users[0]?

No, I make something like:

@users.each do |u|  
…
