Problem with find(:all) with conditions


#1

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.


#2

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