Rails/ActiveRecord order by Array

ActiveRecord中如果想根据自定义的一个数组id集合排序:

ids = [2,1,3]

users = User.where("id in (?)",ids)

results = ids.map{|id| users.detect{|each| each.id == id}}

 

  

posted @ 2017-10-25 19:25  wangyuyu  阅读(201)  评论(0编辑  收藏  举报