0

I am building a shopping cart app in rails. CartItems model has a column for quantity of the type integer and a column for cart_price of the type decimal. For each item added to the cart a new row is added to the database Model name CartItems. The controller retrieves quantity and price successfully. But when multiplying I receive the error message above. Once the multiplication works I want to add the products together to get the subtotal for the cart.

def subtotal
  @cart_content = @cart_item.pluck(:quantity,:cart_price)
  @subtotal = @cart_content.inject(:*)
end

When I remove .inject(:*) from @subtotal the controller retrieves the correct data.

Example output from view for two products, with quantity and price value present

[[3, #BigDecimal:7fc9a9b2d980,'0.1285E3',18(36)>], [1, # BigDecimal:7fc9a9b2d7c8,'0.115E3',9(27)>]]

1
  • Can you edit your question to include the code that sets @cart_item? Commented Nov 19, 2015 at 6:34

3 Answers 3

2

I'm not 100% sure but what you probably wanted to achieve is:

@cart_content.sum { |c| c.inject(:*) } - single reduce won't work because it expects a number not an array

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

4 Comments

thank you for your response this code works and so does Christian Schwartz response below. From a performance perspective would it be better to implement one of the two solutions over the other?
probably Christian's solution is a bit faster. however it may be a better idea to keep computed value in db (no computations when user refreshes page, value recalculated when cart changes its state)
is it possible to create a database column that automatically calculates 'row_total' in the database when a new item is added to the cart, or would I need to calculate 'row_total' in the controller and then pass it to the database along with quantity and cart_price to create a new row?
calculate it manually and store result in total_price, here is an example code how you may achieve it devmynd.com/blog/…
0

You are probably better of adding a column to the model that already contains the value of quantity * cart_price # name it row_total

Then you can easily sum the new column like this:

ModelName.sum(:row_total)

Comments

0

You seem to be trying to multiplying the individual elements, which ruby does not like as they are arrays themselves. As djaszczurowski suggested, I'd recommend summing over the array after multiplying the elements.

Extending on his answer, I'd suggest to replace the inject with the following, as in (at least for me) it is more descriptive of what (I think) you want to do with the code:

@subtotal = @cart_content.sum { |count, price| count * price }

2 Comments

Thank you for your response Christian this code works and so does @djaszczurowski response above. From a performance perspective would it be better to implement one of the two solutions over the other?
I don't think any of those two has any specific performance benefit, if you really want to be sure, you'd have to benchmark it.

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.