简体   繁体   English

Rails 3 gem'地理编码器'搜索功能和模型设计

[英]Rails 3 gem 'geocoder' search functionality and model design

I have a sort of general question about the geocoder gem and how to design my models in rails to facilitate the useful features of the geocoder. 我有一个关于地理编码器宝石的一般性问题,以及如何在导轨中设计我的模型以促进地理编码器的有用功能。

I have an Article model, and Place model. 我有一个文章模型和Place模型。 An Article will always be written about a Place, so an article will always have a place_id. 文章将始终写在一个地方,所以一篇文章总是有一个place_id。 The Place model contains the address of the place and and the :latitude and :longitude columns that geocoder requires. Place模型包含地点的地址以及地理编码器所需的:纬度和:经度列。

geocoder has a method 'nearbys' that allows me to find places nearby a given place, for example @place.nearbys would yield a collection of places nearby @place. 地理编码器有一个方法'nearbys',允许我找到一个给定地点附近的地方,例如@ place.nearbys将产生@place附近的地方集合。 That's great, but I want to be able to find all the articles that are written recently, and written about places nearby. 这很好,但我希望能够找到最近写的所有文章,并写下附近的地方。

I can only think of two ways to do that. 我只能想到两种方法。 Either add a latitude and longitude column to my Article model, which would allow me to search @articles.nearby. 将纬度和经度列添加到我的文章模型中,这样我就可以搜索@ articles.nearby。 The problem with this solution is that it seems like it wouldn't be good model design, since a Place should be responsible for holding the latitude and longitude, not every article written about the place. 这个解决方案的问题在于它似乎不是很好的模型设计,因为Place应该负责保持纬度和经度,而不是每篇关于该地方的文章。

If you can make some suggestions on how I could easily achieve this, that would be great, thatnks! 如果你可以就我如何轻松实现这一点提出一些建议,那就太棒了,那就好了!

couple of options that might work for you? 几个可能适合你的选择?

@places = Place.near(@search, @distance, :order => :distance)
@articles = Article.where("place_id IN (?)", @places).where("published_on > ?", 60.days.ago).limit(3 * @places.length)

lazy load the articles from the places but limit the results 懒惰从地方加载文章但限制结果

@place = Place.find(params[:id])
@place.nearbys(@distance).each do |place|
    place.articles.order("created_at desc").limit(3).each do |article|
        # ...
    end
end

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

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