# Problem with find(:all) with conditions

**URL:** <https://www.ruby-forum.com/t/problem-with-find-all-with-conditions/156359>\
**Category:** Rails\
**Created:** [December 25, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/problem-with-find-all-with-conditions/156359 "2008-12-25T15:36:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/problem-with-find-all-with-conditions/156359/1 "2008-12-25T15:36:31Z")

</div>

Hi all… Merry Christmas

having a strange prob.

```
   @clas=ClassClassteacher.find(:all,:conditions=>"user_id='#

```

{session[:user\_id]}’")

```
  if @clas.nil?
    flash[:notice] = 'You are not enrolled in any class'
    render :layout=>"special"

  else
    puts'@clas has values'

  end

```

when i execute this code even when there are no values for @clas the  
if @clas.nil? condition does not get executed.But if i change the  
query to  
@clas=ClassClassteacher.find(:first,:conditions=\>“user\_id=’#  
{session[:user\_id]}’”)

everything works fine. Can anyone tell me why this is happening.

Thankx.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/problem-with-find-all-with-conditions/156359/2 "2008-12-25T15:55:50Z")

</div>

On 25 Dec 2008, at 14:35, removed\_email\_address@domain.invalid wrote:

> ```
> render :layout=>"special"
> 
> else
> puts'@clas has values'
> 
> end
> 
> ```
> 
> when i execute this code even when there are no values for @clas the  
> if @clas.nil? condition does not get executed.But if i change the  
> query to  
> Because find :all always returns an array and an empty array is not nil

> ```
> @clas=ClassClassteacher.find(:first,:conditions=>"user_id='#
> 
> ```
> 
> {session[:user\_id]}’")

because find :first returns a single object or nil

Fred

PS: interpolating stuff in your conditions like that is yucky. You  
could also probably make the whole thing flow more nicely with an  
appropriate association/named\_scope
