これが私が今していることです:
def get_counts
products = Product.all
a_count, b_count, c_count = 0, 0, 0
products.collect{ |p| a_count+=1 if p.some_attribute == 'a' }
products.collect{ |p| b_count+=1 if p.some_attribute == 'b' }
products.collect{ |p| c_count+=1 if p.some_attribute == 'c' }
return a_count, b_count, c_count
end
これは私には恐ろしくスクリプトのように感じます。inject を使用してみましたが、思いどおりに動作させることができませんでした。誰かがこれを行うためのより良い方法を持っていますか?