-2

I am trying to get the total price which consist of quantity, price, and tax

    @order = current_user.orders.build(order_params)
    @order.product = product
    @order.price = product.price
    @orders = product.price * quantity
    @order.total = @orders * 0.029

The price and quantity sums up the total but when i add the tax percentage it doesn't calculate at all?

2 Answers2

0

You get the wrong result, because type of total is integer, instead float.

You have to change this column type, so you need to generate new migration:

rails g migration change_total_to_be_float_in_orders

This generates migration like that:

class ChangeTotalToBeFloatInOrders < ActiveRecord::Migration[5.0]
  def change
  end
end

Add this line to change method:

change_column :orders, :total, :float

Then run the migration:

rails db:migrate
barmic
  • 1,042
  • 7
  • 15
0

I have to assume that "total" means that you want to get the total of the order including the tax. Using 0.029 means that you would be getting the tax amount of the order, not the actual

Difference here is

25 * 0.029 = 0.725

vs

25 * 1.029 = 25.725

Try this instead.

@order.total = @orders * 1.029

I tested this based on what you posted, and it worked for me.

Ceus
  • 23
  • 2
  • 6