我有一个城市模型,在城市的表演动作中,我想渲染城市中特定位置附近的酒店。城市有_许多地点;正在使用 Geocoder near 方法搜索酒店。
为了添加订单功能,我遵循了 Ryan Bates截屏视频 #228,但这种方法似乎不适用于数组,会出现错误undefined method `order' for #< Array:0x007f960d003430>
cities_controller.rb
helper_method :sort_column, :sort_direction
def show
session[:search_radius] = 2 if session[:search_radius].blank?
@city = City.find(params[:id])
@locations = @city.locations
@hotels = []
@locations.each do |location|
unless location.longitude.blank? || location.latitude.blank?
center_point = [location.latitude, location.longitude]
box = Geocoder::Calculations.bounding_box(center_point, session[:search_radius])
thotels = Hotel.near(center_point, session[:search_radius]).within_bounding_box(box)
else
thotels = Hotel.near(center_point, session[:search_radius])
end
@hotels += thotels if thotels
@hotels = @hotels.uniq
end
@hotels = @hotels.order(sort_column + " " + sort_direction).paginate(:page => params[:page], :per_page => 5)
@json = @locations.to_gmaps4rails
respond_with @json, :location => city_url
end
private
def sort_column
Hotel.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
我的问题是:我应该专注于将数组转换为哈希,还是应该首先创建酒店的哈希,或者找到完全不同的方法来执行排序?