I have two model objects:
Events take place. Objects can have events 1 .. *.
Objects have a place, lat and long, which I use with the Geokit Rails plugin. Here's what these models look like in Rails:
class Event < ActiveRecord::Base
belongs_to :venue
acts_as_mappable :through => :venue
end
and
class Venue < ActiveRecord::Base
has_many :events
acts_as_mappable
end
Very simple! Now I would like to run a query, I would like to find all the events a few miles from a certain area, looking at the Geokit API, I can see that I can use the mapping with: through the association on the Event, and should be able to call geokit-crawlers at the Event! Great (so you can see above, I added actions_as_mappable: through the event).
Here is my ActiveRecord request:
Event.find(:all, :origin => [lat, lng], :select => 'title', :within => 5, :limit => 10)
This is where SQL is generated:
SELECT `events`.`id` AS t0_r0, `events`.`title` AS t0_r1, `events`.`description` AS t0_r2,
`events`.`created_at` AS t0_r3, `events`.`updated_at` AS t0_r4, `events`.`venue_id` AS
t0_r5, `events`.`event_detail_id` AS t0_r6, `events`.`event_detail_type` AS t0_r7,
`venues`.`id` AS t1_r0, `venues`.`name` AS t1_r1, `venues`.`lat` AS t1_r2, `venues`.`lng`
AS t1_r3, `venues`.`created_at` AS t1_r4, `venues`.`updated_at` AS t1_r5 FROM `events` LEFT
OUTER JOIN `venues` ON `venues`.id = `events`.venue_id WHERE
(((venues.lat>51.4634898826039 AND venues.lat>51.5533406301823 AND venues.lng>-
0.197713629915149 AND venues.lng<-0.053351862714855)) AND
((ACOS(least(1,COS(0.898991438708539)*COS(-0.00219095974226756)*COS(RADIANS(venues.lat))*COS(RADIANS(venues.lng))
COS(0.898991438708539)*SIN(-
0.00219095974226756)*COS(RADIANS(venues.lat))*SIN(RADIANS(venues.lng))
SIN(0.898991438708539)*SIN(RADIANS(venues.lat))))*6376.77271)
<= 5)) LIMIT 10
. , , , , , 2 , ! , , :
- , , SELECT ActiveRecord. TITLES , , . , SELECT ! , , , "t1_r1". .
, , (.. JOIN), 10 . , , ? , , .
( ). , , ?
!
(Edit: , Limit , , , .)