我有 2 节课:
class Solution
has_many :solution_votes
class SolutionVote
belongs_to :solution
我的观点是这样的:
Proposed Solution A - 2 votes up - 5 votes down
Proposed Solution B - 1 vote up - 0 votes down
Proposed Solution C - 0 votes up - 0 votes down
Proposed Solution D - 7 votes up - 2 votes down
Proposed Solution E - 3 votes up - 1 vote down
我想要的是按最多 UP 投票对其进行排序,使其看起来像这样:
Proposed Solution D - 7 votes up - 2 votes down
Proposed Solution E - 3 votes up - 1 vote down
Proposed Solution A - 2 votes up - 5 votes down
Proposed Solution B - 1 vote up - 0 votes down
Proposed Solution C - 0 votes up - 0 votes down
到目前为止我有这个:
scope :most_votes_up,
select("solutions.*, count(solution_votes.id) AS votes_count").
where(['solutions.state = ?', 'Proposed']).
joins(:solution_votes).
group("solutions.id").
order("votes_count DESC")
产生这个输出:
Proposed Solution D - 7 votes up - 2 votes down
Proposed Solution A - 2 votes up - 5 votes down
Proposed Solution E - 3 votes up - 1 vote down
Proposed Solution B - 1 vote up - 0 votes down
但是......我仍然遇到的问题是:
1.没有投票的建议解决方案丢失(上面示例中的建议解决方案C从显示的列表中丢失)
2.我如何指定仅对UP的计数投票(现在,它的排序基于哪个提议的解决方案获得了最多的投票(上下),而不仅仅是上升)?
我正在使用 PostGRESQL