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

时间:2013-01-22 22:59:10

标签: ruby-on-rails-3 geocode

我正在尝试让geocoder gem处理已使用has_many关系过滤的对象

喜欢这样

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应该与Profile(两个对象)相同,只是@profiles已被过滤。

如何让它发挥作用?

1 个答案:

答案 0 :(得分:0)

这可能不是很困难,但这就是我开始工作的方式

首先抓住表单字段

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

接下来,我根据他们的角色获取了一个用户列表

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

接下来使用地理编码器(railscast#273)插件我创建了一个列表,其中包含表单中指定位置附近的所有地理编码地址(@findaddress)并获取user_id

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

现在,我可以根据表单字段

指定的角色和地址创建过滤的用户列表
@filteruser = @limituser.where(:id => @findlocations)