This project is archived and is in readonly mode.

#4476 ✓invalid
Dylan Markow

Lazy Loading broken since Beta3?

Reported by Dylan Markow | April 26th, 2010 @ 05:18 AM | in 3.0.2

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?

Comments and changes to this ticket

  • Steven Ringo

    Steven Ringo April 27th, 2010 @ 10:44 AM

    • Assigned user set to “Ryan Bigg”

    Cannot confirm this behaviour.

    -1

  • Ryan Bigg

    Ryan Bigg April 27th, 2010 @ 10:44 AM

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

  • Dylan Markow

    Dylan Markow April 27th, 2010 @ 03:38 PM

    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.

  • Jeremy Kemper

    Jeremy Kemper April 27th, 2010 @ 06:54 PM

    • Milestone cleared.
    • State changed from “new” to “open”
  • Dylan Markow

    Dylan Markow April 27th, 2010 @ 08:03 PM

    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.

  • Jeremy Kemper

    Jeremy Kemper April 27th, 2010 @ 08:35 PM

    • State changed from “open” to “invalid”

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

  • Neeraj Singh

    Neeraj Singh April 27th, 2010 @ 08:57 PM

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

  • Dylan Markow

    Dylan Markow April 27th, 2010 @ 08:59 PM

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

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
    • Importance changed from “” to “High”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages