我有查询结果哈希。
hotels = [#<Hotel id: 42, hotel_name: "Vijay: Resort De Alturas,Candolim", stars: "4">, #<Hotel id: 42, hotel_name: "Vijay: Resort De Alturas,Candolim", stars: "4">, #<Hotel id: 47, hotel_name: "locard", stars: "3", >, #<Hotel id: 48, hotel_name: "testtttt", stars: "1">, #<Hotel id: 41, hotel_name: "Vijay: Krish Holiday Inn,Baga", stars: "2">, #<Hotel id: 43, hotel_name: "Indian hotel", stars: "3">, #<Hotel id: 39, hotel_name: "Vijay: Estrela Do Mar, Calangute", stars: "3">, #<Hotel id: 41, hotel_name: "Vijay: Krish Holiday Inn,Baga", stars: "2">, #<Hotel id: 39, hotel_name: "Vijay: Estrela Do Mar, Calangute", stars: "3">, #<Hotel id: 40, hotel_name: "Estrela Do Mar, Calangute", stars: "3">, #<Hotel id: 44, hotel_name: "Taj hotel", stars: "3">, #<Hotel id: 46, hotel_name: "mobile hotel", stars: "3">, #<Hotel id: 41, hotel_name: "Vijay: Krish Holiday Inn,Baga", stars: "2">, #<Hotel id: 40, hotel_name: "Estrela Do Mar, Calangute", stars: "3">, #<Hotel id: 47, hotel_name: "locard", stars: "3">, #<Hotel id: 45, hotel_name: "The malwa", stars: "3">, #<Hotel id: 40, hotel_name: "Estrela Do Mar, Calangute", stars: "3">, #<Hotel id: 42, hotel_name: "Vijay: Resort De Alturas,Candolim", stars: "4"]
和
hotels.map(&:id)
=> [42, 42, 47, 48, 41, 43, 39, 41, 39, 40, 44, 46, 41, 40, 47, 45, 40, 42]
我想从中删除重复的哈希值hotels
并保持哈希值
hotels.map(&:id)
=> [42, 47, 48, 41, 43, 39, 40, 44, 46, 45]
我试过了
hotels.uniq { |i| i[:id] }.map(&:id)
Hotel Load (0.4ms) SELECT DISTINCT `hotels`.* FROM `hotels` INNER JOIN `package_prices` ON `package_prices`.`hotel_id` = `hotels`.`id` WHERE `hotels`.`searchable` = 1 ORDER BY package_prices.price
=> [42, 48, 41, 39, 43, 46, 44, 45, 47, 40]
但这改变了我想要的顺序[42, 47, 48, 41, 43, 39, 40, 44, 46, 45]