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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 23, 1:27 am, marchmatch <zjiam...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> > 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Oct 23, 1:27 am, marchmatch <zjiam...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > > > 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 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.