@counseling = Counseling.ransack(params[:q])
@counselings = @counseling.result.joins('RIGHT JOIN "subjects" ON "subjects"."id" = "counselings"."subject_id"')
@result = {}
@result[:data] = @counselings.group(row_condition).count
塊引用紅寶石如何強制輸出0時計數沒有行
def self.create_case_sql_for_nested_tree2(foreign_key)
modelClass = foreign_key.sub(/_id3$/, '').camelize.constantize
@when_then_conditions = modelClass.roots.map do |o|
o.children.map do |c|
c.children.map do |g|
idlist = g.self_and_descendants.pluck(:id)
"WHEN subject_id IN(#{idlist.join(',')}) THEN #{g.id}"
end
end
end
"CASE #{@when_then_conditions.join(' ')} ELSE null END"
end
你能更準確的瞭解您的問題。因爲如果沒有找到行,count通常會返回0。 –
select s.id,count(nullif(subject_id,0))來自counselings的AS計數c right加入對象s on c.subject_id = s.id group by s.id order by s.id; – Poo
當我執行上面的SQL輸出顯示0計數的記錄但我不知道如何在紅寶石中實現相同。 – Poo