0

I'm trying to get the geocoder gem to work on an object that has been filtered using a has_many relationship

like so

user.rb
has_one :profile
has_many :roles

name:string

profile.rb
belongs_to :user

latitude,latitude:float, address:string, city:string

role.rb
belongs_to :user
name:string

the problem I'm having is I need to filter the roles to say id => 3

so we have

@limitrole = User.includes(:profile, :roles).where('roles.id' => 3)

this returns an object of limited roles, now grab the profile that contains the geocode stuff

@profiles = @limitrole.collect { |user| user.profile }

This returns an object of addresses limited to the users role

But this won't work

@findlocations = @profiles.near('city, country', 20) (city and country being arbitrary values)

Yet this will work

@findlocations = Profile.near('city, country', 20)

@profiles should be the same as Profile (both objects) it's just @profiles has been filtered.

How do I get this to work?

1 Answers1

0

This might not be railsy but this is how I got it to work

first grabbing the form fields

@findaddress = params[:profile][:profileaddress] + ", " + params[:profile][:profilecity]

Next I grabbed a list of the users based on their role

@limituser = User.includes(:profile, :roles).where('roles.id' => 3)

next using the geocoder (railscast #273) plugin I create a list of all the geocoded addresses that are near the locations specified in the form (@findaddress) and grab the user_id

@findlocations = Profile.near(@findaddress, 20).pluck(:user_id)

Now I can create a list of users filtered by their role and address specified by a form field

@filteruser = @limituser.where(:id => @findlocations)