简体   繁体   中英

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. 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. 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. 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.

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])

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,...)

The idea is to break it up into steps. Assuming user has_one style, and params[:style] is and id of the intended style.

You can do something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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