From 42ef4473777bcfe3b7282c3edbad01e2c24d3391 Mon Sep 17 00:00:00 2001
From: Eliot Sykes <eliotsykes@gmail.com>
Date: Mon, 15 Jul 2013 14:50:26 +0100
Subject: [PATCH] Add preload+joins alternative option for overcoming the known
 issue with includes

In face of this known issue, .joins(...).preload(...) felt like a slightly better option (vs. :select option passed to .near()) for just the particular use case I'm up against where includes would have been useful.

Thought it'd be worth adding to the docs, especially as preload() isn't widely demonstrated in Rails own docs.
---
 README.md | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 98eb8cc0..6abc59f2 100644
--- a/README.md
+++ b/README.md
@@ -740,10 +740,17 @@ When reporting an issue, please list the version of Geocoder you are using and a
 Known Issue
 -----------
 
-You cannot use the `near` scope with another scope that provides an `includes` option because the `SELECT` clause generated by `near` will overwrite it (or vice versa). Instead, try using `joins` and pass a `:select` option to the `near` scope to get the columns you want. For example:
+You cannot use the `near` scope with another scope that provides an `includes` option because the `SELECT` clause generated by `near` will overwrite it (or vice versa). 
 
-    # instead of City.near(...).includes(:venues)
+Instead of using `includes` to reduce the number of database queries, try using `joins` with either the `:select` option or a call to `preload`. Both choices are demonstrated below:
+
+    # Pass a :select option to the near scope to get the columns you want.
+    # Instead of City.near(...).includes(:venues), try:
     City.near("Omaha, NE", 20, :select => "cities.*, venues.*").joins(:venues)
+    
+    # This preload call will normally trigger 2 queries regardless of the number of results; one query 
+    # on hotels, and one query on administrators.  Instead of Hotel.near(...).includes(:administrator), try:
+    Hotel.near("London, UK", 50).joins(:administrator).preload(:administrator)
 
 If anyone has a more elegant solution to this problem I am very interested in seeing it.
 
-- 
GitLab