This project is archived and is in readonly mode.

#4951 ✓invalid

Rails Console Executes Where Queries without lazy loading

Reported by hooptie45 | June 24th, 2010 @ 02:08 AM


ruby-1.9.2-head > @cats = Cat.where(:id=>[1,2,3])
 => [#<Cat id: 1, name: "Ralph", created_at: "2010-06-24 00:29:35", updated_at: "2010-06-24 00:29:35">] 

ruby-1.9.2-head > @cats
 => [#<Cat id: 1, name: "Ralph", created_at: "2010-06-24 00:29:35", updated_at: "2010-06-24 00:29:35">] 

ruby-1.9.2-head > @cats.class
 => ActiveRecord::Relation

Edited by Rohit Arondekar for formating.

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh June 24th, 2010 @ 02:43 AM

    I am not able to follow you. Could you please explain what's the concern here?

  • Rohit Arondekar

    Rohit Arondekar June 24th, 2010 @ 06:29 AM

    hooptie45, I've fixed the formating, please remember to wrap code like things in @@@ :) See for more info.

  • Damien MATHIEU

    Damien MATHIEU June 24th, 2010 @ 07:32 AM

    I don't follow either. Please provide more details.

  • Rohit Arondekar

    Rohit Arondekar June 24th, 2010 @ 10:20 AM

    I think what he is expecting is something like this:

    ruby-1.9.2-head > @cats = Cat.where(:id=>[1,2,3])
     => <#ActiveRecord::Relation whatever fields are used to store a arel relation>
    ruby-1.9.2-head > @cats.all
     => [#<Cat id: 1, name: "Ralph", created_at: "2010-06-24 00:29:35", updated_at: "2010-06-24 00:29:35">]

    I'm guessing that in the console, to help the developer, the queries are indeed executed. But I would like to hear more on this from somebody who knows what is going on! :)

  • Tore Darell

    Tore Darell June 24th, 2010 @ 10:50 AM

    When executing something in IRB, it calls inspect on the resulting object and prints it. ActiveRecord::Relation defines inspect as follows:

    def inspect

    Which means the query gets executed, and then inspect is run on the resulting array. I don't think this is designed with IRB/console in mind, it's just how it's meant to work. It could make sense for Relation to have its own inspect, I don't know.. Someone probably thought about this before and came to the conclusion that it shouldn't.

  • José Valim

    José Valim June 24th, 2010 @ 11:11 AM

    • Assigned user set to “Jeremy Kemper”
  • Eloy Duran

    Eloy Duran June 24th, 2010 @ 11:56 AM

    • Assigned user cleared.

    You can normally start irb with --noinspect, or set IRB.conf[:INSPECT_MODE] from your .irbrc. However, you can not change it interactively and have it take effect immediately.

    In my rewrite of irb for Ruby 1.9/MacRuby, called DietRB, you can change this and have it take effect immediately. For instance:

    irb(main):001:0> def i!; IRB.formatter.inspect = !IRB.formatter.inspect; end
    => nil
    irb(main):002:0> class A; def inspect; 'oelala'; end; end
    => nil
    => oelala
    irb(main):004:0> i!
    => #<FalseClass:0x0>
    => #<A:0x102807f20>
    irb(main):006:0> i!
    => true
    => oelala

    Note that in this case I defined a simple method to toggle it ‘i!’. You could add that to your .irbrc.


  • Eloy Duran

    Eloy Duran June 24th, 2010 @ 11:57 AM

    • Assigned user set to “Jeremy Kemper”

    Argh, reassigning to Jeremy…

  • Jason Gignac

    Jason Gignac June 24th, 2010 @ 04:48 PM

    -1 I would submit there are two conflicting desires here: on the one hand, the behaviour of inspect is what you would expect in this situation - if in any other situation I inspected a response, I'd expect to see it's contents. On the other, there is the desire to us console and have it do lazy loading, I presume so you can do some testing around the lazy loading process in your application. My thoughts woudl be, since you CAN start the console with inspect off, as per Mr. Duran, and then simply inspect manually the objects you wish to test, that the first desire outweighs the second, in my mind. Most of the time, I'm expecting, in console, when I run a query, it is because I want to see the results, not test lazy loading (which I can understand it needing to be tested in specific situations, but which GENERALLY OUGHT to be transparent). I'd say leave the behaviour as is.

  • Emilio Tagua

    Emilio Tagua June 29th, 2010 @ 02:53 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

    Queries are lazy loaded, the thing is that in irb every line is inspected so the query is executed.

    Try running the same in a controller:

     @cats = Cat.where(:id=>[1,2,3])

    And you will find that no query is executed, although doing the same in IRB will, because the line is inspected and therefore to_a is called, query executed, records loaded, etc...

    I'm marking the ticket as invalid, if you still think this is a real problem, please feel free to reopen it.

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=""></a>