Ruby on rails 3 Rails 3地理编码器和对象过滤

Ruby on rails 3 Rails 3地理编码器和对象过滤,ruby-on-rails-3,geocode,Ruby On Rails 3,Geocode,我试图让geocoder gem处理一个使用has_许多关系过滤的对象 像这样 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 我遇到的问题是,我需要过滤角色,说id=>3 所以我们有 @limi

我试图让geocoder gem处理一个使用has_许多关系过滤的对象

像这样

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
我遇到的问题是,我需要过滤角色,说id=>3

所以我们有

@limitrole = User.includes(:profile, :roles).where('roles.id' => 3)
这将返回一个角色有限的对象,现在获取包含地理代码内容的概要文件

@profiles = @limitrole.collect { |user| user.profile }
这将返回仅限于用户角色的地址对象

但这行不通

@findlocations = @profiles.near('city, country', 20) (city and country being arbitrary values)
然而,这将起作用

@findlocations = Profile.near('city, country', 20)
@配置文件应与配置文件(两个对象)相同,只是@profiles已被过滤


我怎样才能让它工作呢?

这可能不太容易,但我就是这样让它工作的

首先抓取表单字段

@findaddress = params[:profile][:profileaddress] + ", " + params[:profile][:profilecity]
接下来,我根据用户的角色抓取了一份用户列表

@limituser = User.includes(:profile, :roles).where('roles.id' => 3)
接下来,使用地理编码器(railscast#273)插件,我创建了一个列表,列出了所有地理编码地址,这些地址位于表单(@findaddress)中指定的位置附近,并获取用户id

@findlocations = Profile.near(@findaddress, 20).pluck(:user_id)
现在,我可以创建一个用户列表,这些用户通过表单字段指定的角色和地址进行筛选

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