Skip to content
Advertisement

Total value of warehouse active record

I have the object Tool and I need to calculate the cost of all the Tools in the warehouse. I can sum the column price Tool.sum(:price) but i need to sum (quantity * price) of all the table Tools. Thanks in advance.

create_table "tools", force: :cascade do |t|
    t.integer "quantity"
    t.string "name"
    t.integer "price"
    t.boolean "avilable"
    t.string "store"
    t.string "comments"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

Advertisement

Answer

Check out the example listed for ActiveRecord::Calculations#calculate.

You can do the following:

  Tool.sum("quantity * price")
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement