2

This is probably simple but instead of this:

current_cart.line_items.sum(:line_item_total)

I prefer to implement it like this:

current_cart.line_items.sum(:line_item_unit_price * :quantity)

What is the best syntax to build a calculated field based on two fields?

2 Answers 2

1

You can use

current_cart.line_items.sum("line_item_unit_price * quantity")

to stick with your second option.

Sign up to request clarification or add additional context in comments.

Comments

0

I think following will work

def total_price
  line_item_unit_price * quantity
end

and then

current_cart.line_items.sum(:total_price)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.