Perhaps I''m an idiot and just don''t get it, and perhaps this question has been asked a million times before, but I''m not really sure how to go about searching for an answer to it: Why does my code Library.find(:all, :conditions => "user_id = #{session[:user].id}") return [] instead of nil when there are no results? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Luke wrote:> Perhaps I''m an idiot and just don''t get it, and perhaps this question > has been asked a million times before, but I''m not really sure how to go > about searching for an answer to it: > > Why does my code Library.find(:all, :conditions => "user_id = > #{session[:user].id}") return [] instead of nil when there are no > results?Thats the way its defined in ActiveRecord. It returns an array of models for the sql results and returns it. So when there are no matches, it returns an empty array. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Raja Venkataraman wrote:> Thats the way its defined in ActiveRecord. It returns an array of models > for the sql results and returns it. So when there are no matches, it > returns an empty array.I guess my question is: why does ActiveRecord define it this way? It would seem to be more consistent with the way everything else works in ruby for a function that has no results to return nil, instead of returning an empty array. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
for consistency with its normal results, i''d think. it usually returns an array, and so code gets written expecting to handle an array, therefore it returns an empty array instead of nil. Luke wrote:> Perhaps I''m an idiot and just don''t get it, and perhaps this question > has been asked a million times before, but I''m not really sure how to go > about searching for an answer to it: > > Why does my code Library.find(:all, :conditions => "user_id = > #{session[:user].id}") return [] instead of nil when there are no > results? > >-- Lance Ivy Web Applications Developer RBS Interactive lance.ivy-eTT0+Q0rEURLdZBKzJmsdNBPR1lH4CV8@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Perhaps I''m an idiot and just don''t get it, and perhaps this question > has been asked a million times before, but I''m not really sure how to go > about searching for an answer to it: > > Why does my code Library.find(:all, :conditions => "user_id > #{session[:user].id}") return [] instead of nil when there are no > results?Because the docs say so :-) Find first: This will return the first record matched by the options used. These options can either be specific conditions or merely an order. If no record can matched, nil is returned. Find all: This will return all the records matched by the options used. If no records are found, an empty array is returned. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Raja Venkataraman wrote: >> Thats the way its defined in ActiveRecord. It returns an array of models >> for the sql results and returns it. So when there are no matches, it >> returns an empty array. > > I guess my question is: why does ActiveRecord define it this way? It > would seem to be more consistent with the way everything else works in > ruby for a function that has no results to return nil, instead of > returning an empty array.I would disagree... For me, if I''m doing a find(:all...) I''m going to expect that 99% of the time I''m going to get back some data... and odds are I''m going to loop through them. So, by returning an empty array I can do that without having to check if the result is nil first and getting that nasty "you tried to call nil.each" error. -philip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---