+1 on James' comment.  This snippet (from a Rails 3 console) might clear
things up a bit:

ruby-1.9.2-p0 > users = User.where('id > 0')
ruby-1.9.2-p0 > users.class
 => ActiveRecord::Relation


You can directly get to the collection by calling the all (or first, last --
any method that returns an object or collection directly) method directly
from the model:

ruby-1.9.2-p0 > collection = User.all
ruby-1.9.2-p0 > collection.class
 => Array


With an ActiveRelation object (users), the SQL will not be execute until we
call an ActiveRecord method on the ActiveRelation object or iterate over
it...

ruby-1.9.2-p0 > collection = users.all
ruby-1.9.2-p0 > collection.class
 => Array

ruby-1.9.2-p0 > users.each{|u| puts u.class}
User
User
** When the ActiveRelation object is iterated over, it will actually execute
the SQL and fetch the object(s)....



As mentioned, you can also build upon the ActiveRelation object:

ruby-1.9.2-p0 > refined_users = users.where("id < 4")
ruby-1.9.2-p0 > refined_users.class
 => ActiveRecord::Relation **Still an ActiveRelation object!

You obviously cannot do that to an array collection!


Rails gives you the choice.  Hope that clears it up a little bit!  This
screencast does a pretty good walk-thorugh:
http://asciicasts.com/episodes/239-activerecord-relation-walkthrough

Cheers

Ben


On Mon, Jul 18, 2011 at 5:45 PM, James Miller <[email protected]> wrote:

> @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
>

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

Reply via email to