rails geocoder near_by结果连接查询返回活动记录关系

时间:2014-03-30 15:39:56

标签: sql ruby-on-rails activerecord rails-geocoder

产生的结果 locations_by_geocoder = Location.near([params [:latitude],params [:longitude]],params [:radius],:units =>:km)

locations_by_where = Location.where(:id => [7,10,9,8]) 是相同的

location_by_geocoder output is

 [#<Location id: 7, country: nil, state: nil, city: nil, locality: nil, name: "Coffee On Canvas", latitude: "12.93516201597191", longitude: "77.63097871416265", created_at: "2014-03-29 15:20:34", updated_at: "2014-03-29 15:20:34", address: "#84, S.T.Bed Layout, 4th Block Koramangala", id_location: "cd49dfa966245b12">,
 #<Location id: 10, country: nil, state: nil, city: nil, locality: nil, name: nil, latitude: "12.93516201597191", longitude: "77.63097871416265", created_at: "2014-03-30 14:47:03", updated_at: "2014-03-30 15:20:52", address: nil, id_location: "60f7f024d37c74f9">,
 #<Location id: 9, country: nil, state: nil, city: nil, locality: nil, name: "Cafe Coffee Day", latitude: "12.93434671368444", longitude: "77.6256083701297", created_at: "2014-03-29 15:41:42", updated_at: "2014-03-29 15:41:42", address: "Mango Suites,", id_location: "8db810b6762fd784">,
 #<Location id: 8, country: nil, state: nil, city: nil, locality: nil, name: "Cafe Coffee Day", latitude: "12.932626125142486", longitude: "77.62338638305664", created_at: "2014-03-29 15:31:53", updated_at: "2014-03-29 15:31:53", address: "BPCL RAJAJINAGAR,Dr.RajkumarRoad, Rajajinagar", id_location: "ff71f5a0b4de9e1b">]

location_by_where output is
  [#<Location id: 7, country: nil, state: nil, city: nil, locality: nil, name: "Coffee On Canvas", latitude: "12.93516201597191", longitude: "77.63097871416265", created_at: "2014-03-29 15:20:34", updated_at: "2014-03-29 15:20:34", address: "#84, S.T.Bed Layout, 4th Block Koramangala", id_location: "cd49dfa966245b12">,
 #<Location id: 8, country: nil, state: nil, city: nil, locality: nil, name: "Cafe Coffee Day", latitude: "12.932626125142486", longitude: "77.62338638305664", created_at: "2014-03-29 15:31:53", updated_at: "2014-03-29 15:31:53", address: "BPCL RAJAJINAGAR,Dr.RajkumarRoad, Rajajinagar", id_location: "ff71f5a0b4de9e1b">,
 #<Location id: 9, country: nil, state: nil, city: nil, locality: nil, name: "Cafe Coffee Day", latitude: "12.93434671368444", longitude: "77.6256083701297", created_at: "2014-03-29 15:41:42", updated_at: "2014-03-29 15:41:42", address: "Mango Suites,", id_location: "8db810b6762fd784">,
 #<Location id: 10, country: nil, state: nil, city: nil, locality: nil, name: nil, latitude: "12.93516201597191", longitude: "77.63097871416265", created_at: "2014-03-30 14:47:03", updated_at: "2014-03-30 15:20:52", address: nil, id_location: "60f7f024d37c74f9">]

但在连接中使用它时会产生不同的结果

   books = books.joins(:location).merge(locations_by_geocoder) 

   produces

[#<Book id: 10, created_at: "2014-03-30 14:47:03", updated_at: "2014-03-30 15:20:52">]



  query syntax is

 SELECT locations.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((12.9398981 - locations.latitude) * PI() / 180 / 2), 2) + COS(12.9398981 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((77.6275559 - locations.longitude) * PI() / 180 / 2), 2))) AS distance, CAST(DEGREES(ATAN2( RADIANS(locations.longitude - 77.6275559), RADIANS(locations.latitude - 12.9398981))) + 360 AS decimal) % 360 AS bearing FROM `books` INNER JOIN `locations` ON `locations`.`id` = `books`.`location_id` WHERE (locations.latitude BETWEEN 12.930904883940814 AND 12.948891316059187 AND locations.longitude BETWEEN 77.61832835502157 AND 77.63678344497843 AND 6371.0 * 2 * ASIN(SQRT(POWER(SIN((12.9398981 - locations.latitude) * PI() / 180 / 2), 2) + COS(12.9398981 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((77.6275559 - locations.longitude) * PI() / 180 / 2), 2))) <= '1') ORDER BY distance ASC
    where as 

    books = books.joins(:location).merge(locations_by_where)

    produces

[#<Book id: 11, title: "ggn", author: "steven jobs,steve jobs", isbn_10: "1572846933", isbn_13: "1932841660", edition: "1", print: nil, publication_year: 2011, publication_month: "2011", condition: "", value: nil, status: nil, stage: nil, description: "", visits: 38, user_id: 5, prefered_place: nil, prefered_time: nil, created_at: "2014-01-01 08:13:00", updated_at: "2014-03-30 14:47:35", rating: nil, image: nil, publisher: nil, goodreads_id: nil, ext_image_url: nil, pages: nil, language_code: nil, barter_type: nil, location_id: 10, id_book: nil>]

and query syntax is 

SELECT `books`.* FROM `books` INNER JOIN `locations` ON `locations`.`id` = `books`.`location_id` WHERE `locations`.`id` IN (7, 10, 9, 8)

即使locations_by_where和locations_by_geocoder的输出相同

两者都是ActiveRecord :: Relation :: ActiveRecord_Relation_Location

1 个答案:

答案 0 :(得分:0)

该问题已连接到locations.near附近的范围

https://github.com/alexreisner/geocoder/issues/627#issuecomment-39048249