Forum: Ruby on Rails Rails3 ActiveRecord lazy loading?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F325896423626af1a63fa6823ea22dd8?d=identicon&s=25 marchmatch (Guest)
on 2010-10-23 16:32
(Received via mailing list)
Hi,

Is Rails3 ActiveRecord supposed to be lazy loading? In the following
snippet
when I call scoped on User model, it does return a
ActiveRecord::Relation
object. But at the same time the query is performed: "User Load (0.3ms)
 SELECT "users".* FROM "users". If I do a u.all after it, no query is
performed. Any thoughts?

ruby-1.9.2-p0 > u=User.scoped
  User Load (0.3ms)  SELECT "users".* FROM "users"
[#<User id: 1, name: "u1", created_at: "2010-09-08 04:08:28",
updated_at:
"2010-09-08 04:08:28">, #<User id: 2, name: "u2", created_at:
"2010-09-08
04:08:34", updated_at: "2010-09-08 04:08:34">, #<User id: 3, name: "u3",
created_at: "2010-09-08 04:08:39", updated_at: "2010-09-08 04:08:39">]

ruby-1.9.2-p0 > u.class
ActiveRecord::Relation < Object
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2010-10-23 16:35
(Received via mailing list)
On Oct 23, 1:27am, marchmatch <zjiam...@gmail.com> wrote:
> Hi,
>
> Is Rails3 ActiveRecord supposed to be lazy loading? In the following snippet
> when I call scoped on User model, it does return a ActiveRecord::Relation
> object. But at the same time the query is performed: "User Load (0.3ms)
> SELECT "users".* FROM "users". If I do a u.all after it, no query is
> performed. Any thoughts?

It's because you're doing this in the console: irb calls inspect on
the result of the expression in order to display it to you, which
triggers the load of the data.

Fred
F325896423626af1a63fa6823ea22dd8?d=identicon&s=25 marchmatch (Guest)
on 2010-10-23 17:36
(Received via mailing list)
Fred, That's right. If I run rails runner "User.scoped" then no query to
db
at all. Thanks for the enlightening info.

On Sat, Oct 23, 2010 at 9:34 AM, Frederick Cheung <
This topic is locked and can not be replied to.