0

I have an array of user info in @user_info and in array there is a column total and column user_id.

I have another instance variable @product and it has column user_id

How do i calculate sum of all values in column total verifying the user_id from @user_info and @product same?

I am trying to calcuate sum in controller, store it in instance variable and use that variable in JSON

Thanks

1 Answer 1

1

Try something like

@user_info.select { |user_info| user_info.user_id == @product.user_id }.sum(&:total)

or, if @user_info is not a simple Array, you should be able to do in one SQL query

@user_info.where(user_id: @product.user_id).sum(:total)
Sign up to request clarification or add additional context in comments.

1 Comment

My pleasure Sir :)

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.