Rails3 ActiveRecord lazy loading?

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

On Oct 23, 1:27am, marchmatch [email protected] 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

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 C. <

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs