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?

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.