Home>
I want to achieve

We are currently building an EC site and are creating a purchase history screen.
As shown in the orders table below, it is assumed that Mr. A (user_id = 1) will buy two products at one time.
If i purchase two or more products at once, I want to display them together in the view (a case often seen on Amazon etc.).

In the case of DB design column, those with ids 4,5,6 in the Orders_Products table are displayed together.
I want to display the one with id 7 separately from 4,5,6 (= purple part becomes two)

problem

Items purchased at the same time as shown in the photo will also be displayed separately.
I want to display items purchased at the same time.

In the show action of controller
① Pull the record where user_id in the orders table is the logged-in user (id: 8,9 in this case)
(2) Turn each record with the each method. (This time it will be turned twice)
③ Since there is one or more order_id for each time in each method, turn it again with each method (this time, order_id: 8 is 3 times, order_id: 9 is 1 time).
I'm thinking of implementing it in the flow, but it didn't work.

↓ Current bad example

DB design Orders table
id address_id user_id
8 Five 1
9 6 1
Orders_Products table
id order_id product_id quantity
Four 8 1 1
Five 8 2 3
6 8 3 1
7 9 Ten 2
code
(users_controller.rb)
def show
    @user = User.find (params [: id])
    @orders = Order.where (user_id: current_user)
 end
(show.html.erb)
                                              <% @ orders.each do | orderr |%>                                         <p>Purchase date:<% = l orderr.created_at, format:: for_result%></p>                                                 <% orders = OrderProduct.where (order_id: orderr.id)%>                        <% orders.each do | order |%>                            <% product = Product.find_by (id: order.product_id)%>                                                                                                                                                    <% images = Image.find_by (product_id: order.product_id)%>                                        <% = image_tag "# {images.name1}", alt: "# {Product.find_by (id: order.product_id) .name}", class: "card-img"%>                                                                                                                                                                     <% = product.name%>                                         <p>                                            <p><% = product.price%><span>Yen/piece</span></p>                                            <% if product.category == "running_shoes"%>                                            <p>Size:<% = cart_item.size%></p>                                            <% end%>                                                                                                <p><% = order.quantity%>pieces</p>                                                <p>Subtotal:<% = product.price * order.quantity%>Yen</p>                                                                                        </p>                                                                                                                                    <% end%>                                                            <% end%>                        
Supplementary information (FW/tool version, etc.)

ruby 2.6.6
rails'~>5.2.4','>= 5.2.4.3'