Forum: Ruby on Rails Problem with find(:all) with conditions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2008-12-25 16:36
(Received via mailing list)
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.
Frederick C. (Guest)
on 2008-12-25 16:55
(Received via mailing list)
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
This topic is locked and can not be replied to.