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)
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
id | address_id | user_id |
---|---|---|
8 | Five | 1 |
9 | 6 | 1 |
id | order_id | product_id | quantity |
---|---|---|---|
Four | 8 | 1 | 1 |
Five | 8 | 2 | 3 |
6 | 8 | 3 | 1 |
7 | 9 | Ten | 2 |
(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'
-
Answer # 1
Related articles
- ruby on rails implementation of purchase history page when two are purchased at the same time
- ruby - [rails] implementation of search function
- ruby - [rails] implementation of sending registration completion email
- ruby - about implementation of tag function in rails
- ruby on rails - implementation of sales ranking function rails
- ruby on rails - implementation of validation function
- ruby - rails tutorial chapter 2 (6th edition)
- ruby on rails - heroku: how to check the database_url used for regular backup
- ruby on rails 5 - i want to update the received parameters with nil
- ruby on rails - it is not saved in the database after registering the product
- ruby on rails - mvc rails
- ruby on rails - rails6 rspec model run-time error nomethoderror: undefined method `valid?'for nil: nilclass
- ruby on rails - rails s can't
- ruby on rails - saved description in nested controller
- ruby on rails - cart function routing error cannot be resolved
- ruby on rails - how to transition from the top page
- ruby on rails - i want to implement a function for administrator users to register luggage for general users in rails
- ruby on rails - things associated with a foreign key cannot be called in view
- ruby on rails 6 - environment construction of ruby on rails
- ruby - passing variables using render partial
- ruby - about associations with 4 models
- ruby - how to implement the edit/update function of multiple tables when using form object in rails
- ruby - all rails gems are not installed
- ruby - output the total integer value held by each user
- ruby - i want to use the search method for active hash data
- ruby - destroy action does not respond
- ruby - how do you see the rails controller relative path? (carrierwave directory deleted)
- ruby - when deploying on capistrano, i get the error "access denied for user'ec2-user' @'localhost' (using password: no)&qu
- javascript - run multiple times with js + each do
I created a combination of the Orders table and the Order_Product table and changed to pulling from Address_id and it worked.