0

考虑一下:

ids = [14195, 6053, 53119, 7513, 5161, 43252, 7633, 627, 54644, 1438]

Foo.find(ids).map(&:id)返回:

[627, 1438, 5161, 6053, 7513, 7633, 14195, 43252, 53119, 54644]

有什么方法可以按照find输入的顺序返回结果吗?IE:

[14195, 6053, 53119, 7513, 5161, 43252, 7633, 627, 54644, 1438]
4

1 回答 1

3

参考this

Foo.where(:id => ids).order("field(id, 627, 1438, 5161, 6053, 7513, 7633, 14195, 43252, 53119, 54644").map(&:id)

或与where

Foo.where(:id => ids).order("field(id, #{ids.join(',')})").map(&:id)

或与find

Foo.find(ids, :order =>"field(id, #{ids.join(',')})").map(&:id)
于 2013-05-31T06:55:13.737 回答