简体   繁体   English

rails geocoder gem查找具有匹配地址的用户

[英]rails geocoder gem find users that have matching address

I'm using the geocoder gem for my rails app that pulls in the longitude and latitude for each users address. 我在我的Rails应用程序中使用了Geocoder gem,它为每个用户地址提取了经度和纬度。 Each profile of the user has an address (and more later). 用户的每个个人资料都有一个地址(以及以后的地址)。 The profile currently has_one address and the address belongs_to the profile. 该配置文件当前具有一个地址,该地址属于该配置文件。

What I want to do is search the area so I am using the near() function which works fine on the controller when I want to search the Address model. 我想做的就是搜索区域,所以我正在使用near()函数,当我想搜索Address模型时,该函数在控制器上工作正常。 It returns to me an @ instance which I can use in the view and I loop through the results pulling the address.profile.suburb or .name etc form each address result. 它给我返回一个@实例,可以在视图中使用它,并循环遍历结果以提取address.profile.suburb或.name等形成每个地址结果。 But this is done in the view. 但这是在视图中完成的。

What I would like to do is have an instance of @users since the next thing I want to do is filter by other models - I have a Style model that if they have checked the filter Style - any user with Style AND has an address near() will return into a @user instance and then I can use that in the view. 我想做的是有一个@users实例,因为我想做的下一件事是由其他模型过滤-我有一个Style模型,如果他们检查了过滤器Style-任何具有Style AND的用户的地址都接近()将返回到@user实例,然后可以在视图中使用它。

Not sure how to do this and everything I try doesn't work - I thought it would be something like User.all.profiles.address.near(params[:search]) 不确定如何执行此操作,我尝试的所有方法均不起作用-我认为这将类似于User.all.profiles.address.near(params [:search])

but then I need to also filter by style which is in a table not connected to address (it's to the user model) 但然后我还需要按样式过滤,该样式在未连接到地址(是用户模型)的表中

or something like that... 或类似的东西...

help appreciated. 帮助赞赏。

The context you provided is rather vague. 您提供的上下文非常模糊。 Is Style a table by itself or a column in the User table? 是样式表本身还是用户表中的列? What's the association between style and user? 样式和用户之间有什么关联? (has_many,has_one,...) (has_many,has_one,...)

The idea is to break it up into steps. 想法是将其分解为多个步骤。 Assuming user has_one style, and params[:style] is and id of the intended style. 假设用户具有has_one样式,并且params[:style]是预期样式的ID。

You can do something like: 您可以执行以下操作:

@users = User.all.profiles.address.near(params[:search])
@users.joins(:style).where('styles.id = ?', params[:style])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM