Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a quick way to do this. These will simply return arrays of ActiveRecord objects.</p> <pre><code>class Area def events Event.joins("INNER JOIN areas ON areas.id=#{id} AND st_contains(areas.geometry, events.geometry)").all end end class Event def areas Area.joins("INNER JOIN events ON events.id=#{id} AND st_contains(areas.geometry, events.geometry)").all end end </code></pre> <p>You probably should memoize (cache the result) so that you don't query the database every time you call the method. That should be straightforward; I leave it as an exercise for the reader.</p> <p>It <em>may</em> be possible to get sophisticated and wrap this up in a true Rails association proxy (so you can get all the Rails association goodies). I haven't looked into this though. It wouldn't be a standard Rails association in any case, because you're not storing IDs.</p> <p>Twelfth is right: you should create spatial indexes for both tables. Activerecord-postgis-adapter should make those easy to do in your migration.</p> <pre><code>change_table :events do |t| t.index :geometry, :spatial =&gt; true end change_table :areas do |t| t.index :geometry, :spatial =&gt; true end </code></pre> <p>If you're having trouble with installing postgis, I recently wrote up a bunch of blog entries on this stuff. Check out <a href="http://www.daniel-azuma.com/blog/archives/category/tech/georails" rel="noreferrer">http://www.daniel-azuma.com/blog/archives/category/tech/georails</a>. I'm also the author of rgeo and activerecord-postgis-adapter themselves, so I'm happy to help if you're stuck on stuff.</p>
 

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