クエリ結果ハッシュがあります。
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]