2011-05-19 18 views
1

我一直在瀏覽mongoid文檔,Geocoder文檔並詢問了一位朋友,但我仍然對此有所瞭解。我有兩個型號,事件和地點:使用地理編碼器的Mongoid查詢

class Event 
    include Mongoid::Document 
    belongs_to :place 
end 

class Place 
    include Mongoid::Document 
    has_many :events 
end 

我已經設置這些使Event.placePlace.events做工精細,是扔我的是結合地理編碼方法附近將查詢的東西。目前Place.near(location)正常工作,正是我想要做的就是查詢活動通過自己的位置,所以Event.place.near(location)

我試着看的東西,如:

@events = Event.where('event.place' => near(location)

@events = Event.where(place.near(location))

任何幫助將不勝感激。

+1

使用這個頁面上的信息,http://stackoverflow.com/questions/4364213/mongoid-querying-by-referenced-document我已經想出了這個解決方案,但是如果可能的話,會喜歡看更優雅的東西: 'place_ids = Place.near(@ocation).map(&:_ id) @events = Event.where(:place_id.in => place_ids' – 2011-05-19 14:24:18

回答

0

這豈不是

place.near(location).events.all 

,你可以查詢像

place.near(location).events.where(.... 
+0

'place.events'只能在分配給var後才起作用,所以'@places = Place.all'然後'@places.each do | place |無法在Place模型上直接調用@events + = place.events結束''.events'。或者我找到了。但是在將模型分配給變量之後,您不能再將其作爲模型進行查詢。 – 2011-05-19 13:58:39