@highschools = HighSchool.scoped will give you the lazy loading equivalent
of .all with the added benefit that you can build on it later.  The query
will be executed at the last possible moment -- usually when you begin
iterating over the collection.

James

On Mon, Jul 18, 2011 at 5:39 PM, Glenn Little <[email protected]> wrote:

> Ah, okay, I see.  That was a subtle one!  .all and .first are actually
> fundamentally different from .where().  But it appears natural to use
> them kind of interchangeably, just depending whether or not you happen
> to have some conditions, e.g., in a controller setting up for a view:
>
> @highschools = HighSchool.all
> @universities = University.where("some condition")
>
> Those variables are *not* of the same class.
>
> Is there a rule of thumb to make this not annoying?  I would have
> thought "just use .where() for consistency", but that leads to the
> where(["1=1"]) sort of hackishness, unless I'm missing something?
>
> -glenn
>
> On Mon, Jul 18, 2011 at 5:19 PM, Guyren Howe <[email protected]> wrote:
> >
> > ActiveRecord in Rails 3 tries to be lazy. So you can queue up all kinds
> of modifiers to your query separately, and then execute them all together
> when you’re ready. .all is one way to trigger the actual query happening,
> and thus the conversion of a lazy query object into a result set.
> >
> > --
> > SD Ruby mailing list
> > [email protected]
> > http://groups.google.com/group/sdruby
> >
>
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
>

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

Reply via email to