Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 453333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:09:41+00:00 2026-05-12T22:09:41+00:00

I have two model objects: Event Venue Events have Venues. Venues can have 1..*

  • 0

I have two model objects:

  • Event
  • Venue

Events have Venues. Venues can have 1..* events.

Venues have a location, a 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’d like to run a query, I’d like to find all the events within a few miles of a certain area, looking at the Geokit API, I see that I can use mappable with a :through association on Event, and should be able to call geokit finders on Event! Excellent (so you can see above, I’ve added acts_as_mappable :through to the Event).

Here’s my ActiveRecord query:

Event.find(:all, :origin => [lat, lng], :select => 'title', :within => 5, :limit => 10)

Here’s the SQL 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

Ouch. Well, this takes quite some time to run, and it’s not just the math, I think there’s a significant problem here with performance, this takes 2 seconds to run on my desktop! But getting down to it, here is my complaint:

  • To try and improve performance, you can see I have tried to use a SELECT statement in my ActiveRecord find query. I just want to know the TITLES of these Events that match, not all the other fields. But the query goes through, and SELECTs everything from Event! It uses strange aliases which I’m not familiar with like ‘t1_r1’. So this clearly is a problem.

Furthermore, this query, when run against the Venues alone (i.e. doing away with the JOIN) is executed in less than 10ms. So is there a way to do this Venues search first, and THEN do the join against the Events? I think the join must be being done on the two tables as a whole, and only then the geocoding part comes in and slims the dataset down.

Any help in tightening up this issue would be much appreciated (this is a non-commercial project). I feel that the model is very simple I should be able to do this without too much fuss?

Thanks in advance!

(Edit: In an early version of this post I was stupid, Limit worked fine, my view was broken, edited to take that part out.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:09:41+00:00Added an answer on May 12, 2026 at 10:09 pm

    It looks like acts_as_mappable is using the eager loading functionality of Rails Active Record. Once you start eager loading any :select argument is ignored and you cannot specify particular columns from a table/model. In your case i think this is happening because of the :through=>:venue argument to acts_as_mappable. Can you make it work without the through argument? You can always poke around the source code as well to see if there are any other adjustments you can make.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arraylist with a number of model objects.I want to find the
I have two objects of the same class (Model) type. I want to check
I have set up a Core Data model where I have two objects, say
I have a an object stored in the model called domain, which has two
I have two model: User, Article A user can like or dislike many articles,
I have two Model's which are related with a ForeignKey field. Let's call these
I have two models, Image and Tag . Each Image object can have more
I have two model classes, say parent and child. Parents are created first, and
I have two questions. 1: At the moment I have two model classes that
I have two models: The model NetworkObject try to describe hosts. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.