Using :through in a belongs_to relation #26

23tux opened this Issue Mar 7, 2011 · 21 comments


None yet


I don't know if this is really a bug, or only not implemented. But is it possible to use a :through statement in geocoded_by? For example, I have two models, a User and a Location model, and I want to get nearby users of a user.

class User < ActiveRecord::Base
belongs_to :location
geocoded_by :location, :through => :location

class Location < ActiveRecord::Base
has_many :users
geocoded_by :address, :latitude => :lat, :longitude => :lon

(btw: I know this isn't really the right syntax, just to make myself clear)
So that I can do sth like this:


I don't know if this is possible, but it would be great! ;)


A few people have asked for this, but it's pretty complicated and there are many more urgent features. I'll leave the issue open and see if anyone votes for it. If you can come up with an implementation (pull request) that would help although, as I said, it's complicated.


If you are using rails 3 you can try this User.join(:location).near(...), that seems to work if you don't have any conflicting column names in the tables.. as far as i can tell.. get strange issues that the then end up being equal to the which I am now trying to figure out why..


@alexreisner: Great work on producing this gem. I'd like to vote for this feature -- it'd be hugely useful to allow geocoder to traverse relations.


Just to add an alternative workaround, I am currently geocoding my child model using address data from the parent as follows:

class Child < ActiveRecord::Base
  belongs_to :parent

  geocoded_by :parent_address
  after_validation :geocode
  def parent_address; parent.address; end

The only downside of this is that changes to the parent model must be propagated to the child.


Definitely need this ability. I guess manual querying for now. I see a pull request to enable this. Has it been reviewed?


I am also using :through in a belongs_to relation and i think this feature will be ideal.


I also want this feature, although I need it for a has_one relationship.


I would also love this feature.


+1 For the feature as well!


I'd love to see this too. Cheers.


+1 would love to see this.


You actually can do this, but it's a little wonky. The way full_column_name() works is that it doesn't qualify the column name if it has a dot, so you can say something like

reverse_geocoded_by "locations.latitude", "locations.longitude"

Then when you say Model.joins(:location).near("Baltimore, MD", 30) you get everything set up right.

Soon, if I have time, I'll make a pull request that add the ability to have through: association option on geocoded_by.


@mgates solution works nicely. E.g.:

class User < ActiveRecord::Base
  belongs_to :location
  reverse_geocoded_by "locations.latitude", "locations.longitude"

class Location < ActiveRecord::Base

users = User.joins(:location).near( [lat, lon], 10)

users.each{ |user| puts "#{} #{user.distance} #{user.bearing}" }

+1. Here is a method I am using to associate with a sibling (parent's child.)


+1 @mgates, your solution is fantastic. Cheers.


+1 @mgates thanks you rock !


Do we still want to see this functionality available out-of-the-box, or is everyone happy with @mgates workaround?


I'd prefer it out of the box, but it's not a priority since the workaround is functional. Happy Thanksgiving!


+1 @mgates your solution worked for me as well! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment