Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 根据数组顺序对rails查询排序_Mysql_Sql_Ruby On Rails_Activerecord - Fatal编程技术网

Mysql 根据数组顺序对rails查询排序

Mysql 根据数组顺序对rails查询排序,mysql,sql,ruby-on-rails,activerecord,Mysql,Sql,Ruby On Rails,Activerecord,我想根据数组中的值对ActiveRecord查询进行排序。比如: @fruits=Fruit.where(seeds: true)._________________________ 假设我想使用数组对结果按颜色排序[“红色”、“蓝色”、“黄色”] 我知道SQL支持使用case语句进行自定义排序,Rails是否有利用它的功能?如果您使用的是MySQL,那么可以使用FIELD。它看起来像: @fruit = Fruit.where(seeds: true).order("FIELD(color,

我想根据数组中的值对ActiveRecord查询进行排序。比如:

@fruits=Fruit.where(seeds: true)._________________________
假设我想使用数组对结果按颜色排序
[“红色”、“蓝色”、“黄色”]


我知道SQL支持使用case语句进行自定义排序,Rails是否有利用它的功能?

如果您使用的是MySQL,那么可以使用
FIELD
。它看起来像:

@fruit = Fruit.where(seeds: true).order("FIELD(color, 'Red', 'Blue', 'Yellow')")

我对此表示怀疑,您可能需要使用一点定制SQL:或者MySQL的
字段
(假设颜色列名为
color