Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Lazy Loading broken since Beta3?

#4476

In Beta 2, lazy loading seemed to function the same as described on Pratik's posting at http://m.onkey.org/2010/1/22/active-record-query-interface. That is, doing something like this works as expected:

cars = Car.where(:colour => 'black') # No Query
cars.each {|c| puts c.name } # Fires "select * from cars where ..."

However, in Beta 3, the first line of code performs the SQL query:

cars = Car.where(:colour => 'black') # Fires "select * from cars where ..."

Is this expected? Is there an option to turn lazy loading back on?

Reported by Dylan Markow · April 26th, 2010 @ 05:18 AM

State: invalid
Milestone: 3.0.2
Assigned to: Ryan Bigg Ryan Bigg
Importance: High

Activity

  1. Steven Ringo
    Steven Ringo
    • Assigned user set to Ryan Bigg

    Cannot confirm this behaviour.

    -1

    April 27th, 2010 @ 10:44 AM

  2. Ryan Bigg
    Ryan Bigg

    I worked with Steve on this and couldn't confirm the behaviour either.

    April 27th, 2010 @ 10:44 AM

  3. Dylan Markow
    Dylan Markow

    Weird -- I just tried it on another computer and same issue. Rails 3.0.0.beta2 does what it should, 3.0.0.beta3 does not. I tried it both on Ruby 1.9.1 and Ruby 1.9.2 (head), as well as both postgres and sqlite (all running on OS X). Here is the actual output from my rails console.

    Beta 2 (no database queries made):

    iMac [~/dev/lazy] rails c
    Loading development environment (Rails 3.0.0.beta2)
    ruby-1.9.1-p378 > posts = Post.order("title")
     => #<ActiveRecord::Relation:0x000001036b68e0 @table=#<Arel::Table:0x000001040a9c68 @name="posts", @table_exists=nil, @options={:engine=>#<Arel::Sql::Engine:0x0000010439e330 @ar=ActiveRecord::Base, @adapter_name="PostgreSQL">}, @engine=#<Arel::Sql::Engine:0x0000010439e330 @ar=ActiveRecord::Base, @adapter_name="PostgreSQL">>, @klass=Post(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime), @implicit_readonly=nil, @loaded=nil, @limit_value=nil, @offset_value=nil, @lock_value=nil, @readonly_value=nil, @create_with_value=nil, @from_value=nil, @includes_values=[], @eager_load_values=[], @preload_values=[], @select_values=[], @group_values=[], @order_values=["title"], @joins_values=[], @where_values=[], @having_values=[], @arel=nil, @scope_for_create=nil, @order_clause=nil, @to_sql=nil, @last=nil, @first=nil, @join_dependency=nil, @should_eager_load=nil, @records=[]>
    

    Beta 3 (database query made):

    iMac [~/dev/lazy] rails c
    Loading development environment (Rails 3.0.0.beta3)
    ruby-1.9.1-p378 > posts = Post.order("title")
     => [#<Post id: 1, title: "Post 1", body: "text", created_at: "2010-04-27 14:01:22", updated_at: "2010-04-27 14:01:22">, #<Post id: 2, title: "Post 2", body: "text", created_at: "2010-04-27 14:01:22", updated_at: "2010-04-27 14:01:22">, #<Post id: 3, title: "Post 3", body: "text", created_at: "2010-04-27 14:01:22", updated_at: "2010-04-27 14:01:22">, #<Post id: 4, title: "Post 4", body: "text", created_at: "2010-04-27 14:01:22", updated_at: "2010-04-27 14:01:22">, #<Post id: 5, title: "Post 5", body: "text", created_at: "2010-04-27 14:01:22", updated_at: "2010-04-27 14:01:22">]
    

    Please let me know what other details I could provide, or if you want me to try anything else specifically.

    April 27th, 2010 @ 03:38 PM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone cleared.
    • State changed from new to open

    April 27th, 2010 @ 06:54 PM

  5. Dylan Markow
    Dylan Markow

    After playing with it some more this morning, it appears that this is only happening in the console. Running the actual rails application server results in no database queries until I actually try to use the results.

    So it seems that lazy loading is not enabled in the rails console as of Beta 3, but still works perfectly fine in a running app.

    April 27th, 2010 @ 08:03 PM

  6. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to invalid

    irb calls inspect on the result of a command, which will kick off a query.

    April 27th, 2010 @ 08:35 PM

  7. Neeraj Singh
    Neeraj Singh

    @Dylan

    As Jeremy said irb is calling inpsect on the value. If you want to suppress than then try

    > posts = Post.order("title"); nil
    

    Notce nil at the end. In this way irb will not invoke inspect on posts. Hope that helps.

    April 27th, 2010 @ 08:57 PM

  8. Dylan Markow
    Dylan Markow

    Thanks! I saw a recent commit for the Relation#inspect method, but didn't put two and two together since I had no idea that irb calls #inspect (though it makes perfect sense now that I know).

    April 27th, 2010 @ 08:59 PM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to High

    October 15th, 2010 @ 11:01 PM