I have the following distance table:
ββββββ¦βββββββββββββ¦βββββββββββββ¦ββββββββββββββββββ¦ββββββββββββββββββ¦βββββββββββ β id β origin_lat β origin_lng β destination_lat β destination_lng β distance β β βββββ¬βββββββββββββ¬βββββββββββββ¬ββββββββββββββββββ¬ββββββββββββββββββ¬βββββββββββ£ β 1 β 1.234567 β 2.345678 β 3.456789 β 4.567890 β 10 β β 2 β 5.678901 β 6.789012 β 7.890123 β 8.901234 β 20 β ββββββ©βββββββββββββ©βββββββββββββ©ββββββββββββββββββ©ββββββββββββββββββ©βββββββββββ
Question: how can I create the following SQL query (supported by PostgreSQL) using ActiveRecord and Arel if necessary:
SELECT * FROM distances WHERE (origin_lat, origin_lng) IN ((1.234567, 2.345678), (5.678901, 6.789012)) AND (destination_lat, destination_lng) IN ((3.456789, 4.567890), (7.890123, 8.901234));
I tried this, but it does not work:
Distance.where('(origin_lat, origin_lng) IN (?) AND (destination_lat, destination_lng) IN (?)', [[1.234567, 2.345678], [5.678901, 6.789012]], [[3.456789, 4.567890], [7.890123, 8.901234]])
It generates this:
SELECT "distances".* FROM "distances" WHERE ((origin_lat, origin_lng) IN ('--- - 1.234567 - 2.345678 ','--- - 5.678901 - 6.789012 ') AND (destination_lat, destination_lng) IN ('--- - 3.456789 - 4.56789 ','--- - 7.890123 - 8.901234 '))
And picks up PG::FeatureNotSupported: ERROR: input of anonymous composite types is not implemented
The number of parameters is a variable, so I cannot simply program the query as follows:
Distance.where('(origin_lat, origin_lng) IN ((?,?),(?,?)) AND (destination_lat, destination_lng) IN ((?,?),(?,?))', 1.234567, 2.345678, 5.678901, 6.789012, 3.456789, 4.567890, 7.890123, 8.901234)
Will I need to switch to plain SQL?: /