gpt4 book ai didi

arrays - 在其上应用事件记录 "where"时保持原始数组顺序

转载 作者:行者123 更新时间:2023-12-05 01:24:59 25 4
gpt4 key购买 nike

只是一个问题,我有一个带有一些 ID 的数组 array = [19, 5, 1, 4]当我在上面打电话时 Delivery.where(id: array.map(&:id))要有一个事件记录关系而不是一个数组,“where”语句正在对 id 进行排序,并给我所有按 id 排序的对象: #<ActiveRecord::Relation [#<Delivery id: 1, ... >, #<Delivery id: 4, ... >, #<Delivery id: 5, ... >, #<Delivery id: 19, ... >有谁知道如何保持原来的顺序吗?

谢谢!

最佳答案

ActiveRecord.find——当用数组调用时——将自动以完全相同的顺序返回记录。

只需将您的查询更改为:

Delivery.find(array.map(&:id))

引自docs :

NOTE: The returned records are in the same order as the ids you provide. [...]

关于arrays - 在其上应用事件记录 "where"时保持原始数组顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71015750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com