gpt4 book ai didi

ruby-on-rails - rails : how do I unscope a uniq call?

转载 作者:行者123 更新时间:2023-11-29 13:20:16 25 4
gpt4 key购买 nike

我有一个关系,其中 uniq 被调用为:

rel = Car.where(brand: 'Audi').order(:price).uniq

现在我想取消 uniq (DISTINCT) 部分的作用域,这样我就可以:

rel.limit(30).pluck(:id)

因为这会给我这样的错误:

ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

这是由于查询调用:SELECT DISTINCT(id) FROM cars WHERE brand = 'Audi' ORDER BY price 我希望它执行的操作是:SELECT id FROM cars WHERE brand = 'Audi' ORDER BY price - 但是我无法弄清楚如何仅取消停止 DISTINCT 部分?

最佳答案

您可以使用 distinct(false) 来覆盖 uniq:

rel = Car.where(brand: 'Audi').order(:price).uniq
rel.distinct(false).limit(30).pluck(:id)

关于ruby-on-rails - rails : how do I unscope a uniq call?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256243/

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