2014-03-30 34 views
0

产生的结果locations_by_geocoder = Location.near([params [:latitude],params [:longitude]],params [:radius],:units => km )rails geocoder near_by结果连接查询返回活动记录关系

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 ::关系:: ActiveRecord_Relation_Location

+0

你最终是如何解决的?我有类似的问题 –

回答