Rails: 如何根据条件关联模型的字段对模型进行排序?
Rails: How to sort models by an associated model's field with condition?
我有三个模型:
class Unit < ApplicationRecord
has_many :feedback_units
has_many :feedbacks, through: :feedback_units
end
class Feedback < ApplicationRecord
has_many :feedback_units
has_many :units, through: :feedback_units
end
class FeedbackUnit < ApplicationRecord
belongs_to :unit
belongs_to :feedback
end
这就是我想要的:
反馈模型有一个结果字段。如果结果为真或假,则关闭反馈。如果结果为零,则反馈未关闭。我想从未关闭反馈数量最多的单元到最少的单元进行排序。即使单位没有反馈也应该在列表的底部。
例如:
Unit
Number of Closed Feedbacks
Number of Unclosed Feedbacks
Total Number of Feedbacks
Unit_A
14
5
19
Unit_B
35
23
58
Unit_C
112
17
129
Unit_D
0
0
0
Unit_E
36
0
36
Unit_F
0
19
19
Unit_G
0
0
0
应该排序如下:
Unit
Number of Closed Feedbacks
Number of Unclosed Feedbacks
Total Number of Feedbacks
Unit_B
35
23 <-
58
Unit_F
0
19 <-
19
Unit_C
112
17 <-
129
Unit_A
14
5 <-
19
Unit_D
0
0 <-
0
Unit_E
36
0 <-
36
Unit_G
0
0 <-
0
没有未关闭反馈的单元如何排序并不重要。最后三个单元可以排序如下:
Unit
Number of Closed Feedbacks
Number of Unclosed Feedbacks
Total Number of Feedbacks
.
.
.
.
Unit_E
36
0
36
Unit_D
0
0
0
Unit_G
0
0
0
我试过这样做,但没有成功:
Unit.distinct
.left_joins(:feedbacks)
.group('feedbacks.result IS NULL')
.order('feedbacks.result IS NULL ASC, COUNT(feedbacks.result IS NULL)')
这应该在单个查询和 return ActiveRelation 中。
我该怎么做?
Unit.left_joins(:feedbacks).group("units.id").select(
'units.id,
sum(case when feedbacks.result is null and feedbacks.id is not null then 1 else 0 end) AS unclosed,
sum(case when feedbacks.result is not null then 1 else 0 end) AS closed,
sum(case when feedbacks.id is not null then 1 else 0 end) as total').order('unclosed DESC')
这将适用于 PostgreSQL 和 MySql
我有三个模型:
class Unit < ApplicationRecord
has_many :feedback_units
has_many :feedbacks, through: :feedback_units
end
class Feedback < ApplicationRecord
has_many :feedback_units
has_many :units, through: :feedback_units
end
class FeedbackUnit < ApplicationRecord
belongs_to :unit
belongs_to :feedback
end
这就是我想要的:
反馈模型有一个结果字段。如果结果为真或假,则关闭反馈。如果结果为零,则反馈未关闭。我想从未关闭反馈数量最多的单元到最少的单元进行排序。即使单位没有反馈也应该在列表的底部。
例如:
Unit | Number of Closed Feedbacks | Number of Unclosed Feedbacks | Total Number of Feedbacks |
---|---|---|---|
Unit_A | 14 | 5 | 19 |
Unit_B | 35 | 23 | 58 |
Unit_C | 112 | 17 | 129 |
Unit_D | 0 | 0 | 0 |
Unit_E | 36 | 0 | 36 |
Unit_F | 0 | 19 | 19 |
Unit_G | 0 | 0 | 0 |
应该排序如下:
Unit | Number of Closed Feedbacks | Number of Unclosed Feedbacks | Total Number of Feedbacks |
---|---|---|---|
Unit_B | 35 | 23 <- | 58 |
Unit_F | 0 | 19 <- | 19 |
Unit_C | 112 | 17 <- | 129 |
Unit_A | 14 | 5 <- | 19 |
Unit_D | 0 | 0 <- | 0 |
Unit_E | 36 | 0 <- | 36 |
Unit_G | 0 | 0 <- | 0 |
没有未关闭反馈的单元如何排序并不重要。最后三个单元可以排序如下:
Unit | Number of Closed Feedbacks | Number of Unclosed Feedbacks | Total Number of Feedbacks |
---|---|---|---|
. | . | . | . |
Unit_E | 36 | 0 | 36 |
Unit_D | 0 | 0 | 0 |
Unit_G | 0 | 0 | 0 |
我试过这样做,但没有成功:
Unit.distinct
.left_joins(:feedbacks)
.group('feedbacks.result IS NULL')
.order('feedbacks.result IS NULL ASC, COUNT(feedbacks.result IS NULL)')
这应该在单个查询和 return ActiveRelation 中。
我该怎么做?
Unit.left_joins(:feedbacks).group("units.id").select(
'units.id,
sum(case when feedbacks.result is null and feedbacks.id is not null then 1 else 0 end) AS unclosed,
sum(case when feedbacks.result is not null then 1 else 0 end) AS closed,
sum(case when feedbacks.id is not null then 1 else 0 end) as total').order('unclosed DESC')
这将适用于 PostgreSQL 和 MySql