sql - How to create a multi-column IN predicate with ActiveRecord? -
i have following 'distances' 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 ║ ╚════╩════════════╩════════════╩═════════════════╩═════════════════╩══════════╝
the question is, how can create following sql query (supported postgresql) activerecord, , arel, if necessary:
select * distances (origin_lat, origin_lng) in ((1.234567, 2.345678), (5.678901, 6.789012)) , (destination_lat, destination_lng) in ((3.456789, 4.567890), (7.890123, 8.901234));
i tried this, doesn't work:
distance.where('(origin_lat, origin_lng) in (?) , (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".* "distances" ((origin_lat, origin_lng) in ('--- - 1.234567 - 2.345678 ','--- - 5.678901 - 6.789012 ') , (destination_lat, destination_lng) in ('--- - 3.456789 - 4.56789 ','--- - 7.890123 - 8.901234 '))
and raises pg::featurenotsupported: error: input of anonymous composite types not implemented
the number of parameters variable, can't hard-code query this:
distance.where('(origin_lat, origin_lng) in ((?,?),(?,?)) , (destination_lat, destination_lng) in ((?,?),(?,?))', 1.234567, 2.345678, 5.678901, 6.789012, 3.456789, 4.567890, 7.890123, 8.901234)
am going need drop plain sql? :/
i'm guessing has little more this:
element.where('(origin_lat, origin_lng) in ((?,?),(?,?)) , (destination_lat, destination_lng) in ((?,?),(?,?))', 1.234567, 2.345678, 5.678901, 6.789012, 3.456789, 4.567890, 7.890123, 8.901234)
Comments
Post a Comment