Just fyi, there's a gotcha with this.  If you're using the "give find() a list
of id's" format, using :all happily ignores the list of ids and just returns
all records:
  
  id_list = [1,3,5]
  Person.find(id_list).map{|p| p.id}
  => [1,3,5]
  Person.find(:all, id_list).map{|p| p.id}
  => [1,2,3,4,5,6,7....]

Maybe obvious, since this is in some sense a "different" find() call
where :all or :first don't make sense.  But it does mean that different
usages of find may need to be treated differently when trying to 
assure that their results are array-ized.

        -glenn


Jordan Fowler wrote:
> I'd say if you'd always like an array from ActiveRecord::Base#find, to 
> use the :all option.
> 
> --Jordan
> 
> On Mon, Aug 18, 2008 at 3:43 PM, Glenn Little <[EMAIL PROTECTED] 
> <mailto:[EMAIL PROTECTED]>> wrote:
> 
> 
>     We have a number of places where we use the to_a method on the
>     return of an ActiveRecord find call.  For instance:
> 
>      people = Person.find(blah blah).to_a
> 
>     This way, whether one or more Person objects are returned, "people"
>     is always an array of 0 or more elements.
> 
>     I notice now though that I'm getting warnings:
> 
>      warning: default 'to_a' will be obsolete
> 
>     I believe this is from Ruby, and applies when an object does
>     not define its own to_a method.  I believe this is only the case
>     when my find() happens to return 1 element (an ActiveRecord object
>     instead of an array of ActiveRecord objects), so I guess the
>     ActiveRecord objects are not defining their own to_a?
> 
>     Is there a different "best practice" I might want to follow in the
>     above case so that I always have an array as a find() result?
> 
>     Thanks!
> 
>            -glenn
> 
> 
> 
> 
> 
> -- 
> Jordan A. Fowler
> 2621 First Ave Apt 5
> San Diego, CA 92103
> E-mail: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> Website: http://www.jordanfowler.com
> Office Phone: (619) 618-0355
> Cell Phone: (406) 546-8055
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to