This project is archived and is in readonly mode.
Rails Console Executes Where Queries without lazy loading
Reported by hooptie45 | June 24th, 2010 @ 02:08 AM
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.
Comments and changes to this ticket
-
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 June 24th, 2010 @ 06:29 AM
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.
-
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 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 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.
-
José Valim June 24th, 2010 @ 11:11 AM
- Assigned user set to Jeremy Kemper
-
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 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,
Eloyhttp://rubygems.org/gems/dietrb
http://github.com/alloy/dietrb -
Eloy Duran June 24th, 2010 @ 11:57 AM
- Assigned user set to Jeremy Kemper
Argh, reassigning to Jeremy…
-
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 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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>