Created
May 23, 2011 06:30
-
-
Save tsujigiri/986323 to your computer and use it in GitHub Desktop.
mcmire's Geokit branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Location < ActiveRecord::Base | |
has_many :foos | |
acts_as_mappable default_units: :kms | |
end | |
class Foo < ActiveRecord::Base | |
belongs_to :location | |
acts_as_mappable through: :location | |
end | |
foos = Foo.geo_scope(origin: "berlin") | |
foos.first.distance | |
# => NoMethodError: undefined method `distance' for #<Foo:0x00000005ba50b8> |
Thanks for the insights! Got it working now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See test/database.yml -- the test_helper loads this. If you look in test/boot.rb there's a constant,
ADAPTER
, which gets set based onENV["DB"]
. So when running the tests you'll want to passDB=postgres
on the command line.However, see below...
Not quite. It's been a while since I've looked at this so I had to take another look at the code.
distance
isn't an attribute -- I'm not sure what the proper name for it is but it's kind of a virtual column (from the perspective of SQL).The way the original geokit-rails plugin worked is, if you referred to
distance
within a regular.find
query, when the query was executed, "distance" would literally get replaced with a bunch of SQL which corresponds to the mathematical formula to calculate the distance geographically between two coordinates (specifically, measure the distance between the coordinates of each location in your table).This is also how it works in the Rails 3 version of geokit-rails, except now that the geokit-rails specific stuff is wrapped in ActiveRecord scopes, it has to work a bit differently. Now when you call
.geo_scope
, five things happen:.geo_scope
(:within, :beyond, :range, bounds) are converted into calls to.where
on this new arel instance..where
that "distance" is replaced with the distance formula SQL..where
after the call to.geo_scope
which contain "distance", and it will be replaced appropriately..order
after the call to.geo_scope
(or any method called after.geo_scope
) which contains "distance", and it will be replaced appropriately.So to summarize,
geo_scope
doesn't adddistance
to the records that get returned -- it's merely used in your query.