gpt4 book ai didi

ruby - Distinct 不适用于 select 子句

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

我有两张 table

class DepartmentBudget < ApplicationRecord
has_many :budget_details, dependent: :destroy
end

class BudgetDetail < ApplicationRecord
belongs_to :department_budget
end

每个部门都有特定时间段的预算。每个部门都有许多 budget_details,其中包含所有贷方和借方条目并保持余额。

问题是我想根据最新预算明细的余额对部门预算进行排序。

DepartmentBudget
.joins(:budget_details)
.select('budget_details.balance, department_budgets.*')
.order("budget_details.balance ASC")
.distinct

我已经实现了上述查询,但它返回了重复记录。问题出在 select 子句上,如果我删除 select 则 distinct 有效,但我必须将 select 与 distinct 一起使用。我们将不胜感激。

最佳答案

DepartmentBudget.includes(:budget_details).order("budget_details.balance ASC")

关于ruby - Distinct 不适用于 select 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52399334/

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