0

我正在尝试让地理编码器 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 已被过滤。

我怎样才能让它工作?

4

1 回答 1

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) 
于 2013-01-23T19:02:25.320 回答