mayukarunasekara-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Dec-25 14:35 UTC
Problem with find(:all) with conditions
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 25 Dec 2008, at 14:35, mayukarunasekara-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > 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 toBecause 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> everything works fine. Can anyone tell me why this is happening. > > Thankx. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---