Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get records from SQLite in random order using the Sequel ORM?

Trying to convert this SQLite query

SELECT * FROM `terms` ORDER BY RANDOM() LIMIT 10

to work with a Sequel model. The closest I got:

Term.order(rand{}).limit(10)
Term.order('random ()').limit(10)

which translate into

<Sequel::SQLite::Dataset: "SELECT * FROM `terms` ORDER BY 0.6160215951854449 LIMIT 10">
<Sequel::SQLite::Dataset: "SELECT * FROM `terms` ORDER BY 'random ()' LIMIT 10">

but neither works. Is there a way to pass a SQLite- or other database-specific functions to Sequel's order()?

like image 326
Arman H Avatar asked Dec 15 '22 07:12

Arman H


1 Answers

Using a Sequel.lit expression worked:

Term.order(Sequel.lit('RANDOM()')).limit(10)
like image 105
Arman H Avatar answered Jan 29 '23 20:01

Arman H