Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to transparently implement the Future pattern for ActiveRecord queries in Ruby 1.9?
    primarykey
    data
    text
    <p>I'm working on an existing Rails 2 site with a large codebase that recently updated to Ruby 1.9.2 and the mysql2 gem. I've noticed that this setup allows for non-blocking database queries; you can do <code>client.query(sql, :async =&gt; true)</code> and then later call <code>client.async_result</code>, which blocks until the query completes.</p> <p>It seems to me that we could get a performance boost by having all <code>ActiveRecord</code> queries that return a collection decline to block until a method is called on the collection. e.g.</p> <pre><code>@widgets = Widget.find(:all, :conditions=&gt; conditions) #sends the query do_some_stuff_that_doesn't_require_widgets @widgets.each do #if the query hasn't completed yet, wait until it does, then populate @widgets with the result. Iterate through @widgets ... </code></pre> <p>This could be done by monkey-patching <code>Base::find</code> and its related methods to create a new database client, send the query asynchronously, and then immediately return a Delegator or other proxy object that will, when any method is called on it, call <code>client.async_result</code>, instantiate the result using <code>ActiveRecord</code>, and delegate the method to that. <code>ActiveRecord</code> association proxy objects already work similarly to implement ORM.</p> <p>I can't find anybody who's done this, though, and it doesn't seem to be an option in any version of Rails. I've tried implementing it myself and it works in console (as long as I append <code>; 1</code> to the line calling everything so that <code>to_s</code> doesn't get called on the result). But it seems to be colliding with all sorts of other magic and creating various problems.</p> <p>So, is this a bad idea for some reason I haven't thought of? If not, why isn't it the way <code>ActiveRecord</code> already works? Is there a clean way to make it happen?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload