1
使用我當前的設置,我需要添加一些自定義SQL語句,它可以用Tire Gem來顯示一些數據。在彈性搜索和輪胎中使用SQL
我使用Rails 3.2的Ruby 1.9.3
在我的房屋控制器,我有以下幾點:
@listings = Listing.search()
對於我Listing.rb,我使用的是與搜索方法一些過濾器,如:
def self.search(params={})
tire.search(load: true, page: params[:page], per_page: 50) do |search|
search.query { string params[:query], :default_operator => "AND" } if params[:query].present?
search.filter :range, posted_at: {lte: DateTime.now}
search.filter :term, "property.rooms_available" => params[:property_rooms_available] if params[:property_rooms_available].present?
search.filter :term, "property.user_state" => params[:property_user_state] if params[:property_user_state].present?
...
end
我需要做的就是添加這個SQL語句轉換成搜索方法,使其作用域由lonitude和latitute。該 'COORDS' 由參數傳遞在URL形式
http://localhost:3000/listings?coords=51.0000,-01.0000 52.0000,-02.0000
(存在-01.0000和52.0000之間的空格。)
目前我有:
我想過試圖通過這樣的東西來在控制器範圍內?
def self.polyed(coords)
joins(:property).
where("ST_Within(ST_GeometryFromText('POINT(' || properties.lat || ' ' || properties.lng || ')'),ST_GeometryFromText('POLYGON((#{coords}))'))").
select("title, properties.id, properties.lng,properties.lat")
end
這...
listings_controller.rb
def index
@listings = Listing.polyed(poly_coordinates).search()
end
它需要通過HTML和JSON格式
http://localhost:3000/listings.json?
我返回的結果爲@listings m已經使用RABL來自動生成json。
這可能嗎?
非常感謝提前。
瑞安