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.

Rails Console Executes Where Queries without lazy loading

#4951

Example:

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.

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

State: invalid
Milestone: none
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh

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

    June 24th, 2010 @ 02:43 AM

  2. Rohit Arondekar
    Rohit Arondekar

    hooptie45, I've fixed the formating, please remember to wrap code like things in @@@ :) See http://help.lighthouseapp.com/faqs/getting-started/how-do-i-format-... for more info.

    June 24th, 2010 @ 06:29 AM

  3. Damien MATHIEU
    Damien MATHIEU

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

    June 24th, 2010 @ 07:32 AM

  4. Rohit Arondekar
    Rohit Arondekar

    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! :)

    June 24th, 2010 @ 10:20 AM

  5. Tore Darell
    Tore Darell

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

    def inspect
      to_a.inspect
    end
    

    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.

    June 24th, 2010 @ 10:50 AM

  6. José Valim
    José Valim
    • Assigned user set to Jeremy Kemper

    June 24th, 2010 @ 11:11 AM

  7. Eloy Duran
    Eloy Duran
    • 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
    irb(main):003:0> A.new
    => oelala
    irb(main):004:0> i!
    => #<FalseClass:0x0>
    irb(main):005:0> A.new
    => #<A:0x102807f20>
    irb(main):006:0> i!
    => true
    irb(main):007:0> A.new
    => oelala
    

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

    HTH,
    Eloy

    http://rubygems.org/gems/dietrb
    http://github.com/alloy/dietrb

    June 24th, 2010 @ 11:56 AM

  8. Eloy Duran
    Eloy Duran
    • Assigned user set to Jeremy Kemper

    Argh, reassigning to Jeremy…

    June 24th, 2010 @ 11:57 AM

  9. Jason Gignac
    Jason Gignac

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

    June 24th, 2010 @ 04:48 PM

  10. Emilio Tagua
    Emilio Tagua
    • 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.

    June 29th, 2010 @ 02:53 PM