2012-04-02 14 views
0

以下の方法では、ol.price * ol.committedを実行します。価格は、私はこれを持って進(40.00)私の見解でPostgres - Railsで文字列として返される計算

def self.find_for_order(order) 
    if order 
     select("ol.*, p.name as name, (ol.price * ol.committed) as sku_total, p.price as price, p.style_number AS product_style_number, p.name"). 
     from("order_lines ol"). 
     joins("LEFT JOIN skus s ON s.sku_number = ol.style_number"). 
     joins("LEFT JOIN products p ON p.id = s.product_id"). 
     where("ol.order_id = #{order}"). 
     group("ol.id, p.price, product_style_number, name, ol.style_number") 
    end 
    end 

です:

number_to_currency(@order_lines.map(&:sku_total).sum) 

これが結果です:

$500,400.00 

私はそれで検査を実行する場合、私はこれを取得します:

["500", "400"] 

だから、文字列だと思うので、jそれらを追加するのではなく一緒に叩いてください。任意のアイデアをどのようにこれを修正するには?

答えて

1

数値を数値にキャストします。

number_to_currency(@order_lines.map{|ol| ol.sku_total.to_f }.sum) 
+0

ありがとうございます!これを避けるために私ができることはありますか?私はMySQLからPostgresに移行しようとしています。これらのタイプのものは私を少し緊張させます。 – fatfrog

+0

select句で計算された値を倍精度にキャストし、それがARが結果の型をより自然に理解するのを助けるかどうかを調べることができます。 'select ...(ol.price * ol.committed)::倍精度としてのsku_total、...' – dbenhur

関連する問題