This project is archived and is in readonly mode.
Rails Console Executes Where Queries without lazy loading
-
Neeraj Singh
I am not able to follow you. Could you please explain what's the concern here?
-
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.
-
Damien MATHIEU
I don't follow either. Please provide more details.
-
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! :)
-
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 endWhich 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
- Assigned user set to Jeremy Kemper
-
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 => oelalaNote that in this case I defined a simple method to toggle it ‘i!’. You could add that to your .irbrc.
HTH,
Eloyhttp://rubygems.org/gems/dietrb
http://github.com/alloy/dietrb -
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.
-
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.