I used the exists? Method in the conditional expression and wrote the code to output the SoldOut character to the product when the column name (item_id) in the specified table and the sold product (item.id) match.
qiita.rb
<% if Order.exists?(item_id: [item.id]) %>
<div class='sold-out'>
<span>Sold Out!!</span>
</div>
<% end %>
It may be obvious in my code writing style, but if there was a purpose to make this kind of behavior, this behavior had the purpose to display Sold Out on the sold products.
For that purpose, I didn't suddenly find the exists? Method, but first I bought the item and saved the item_id with the item information in the order table, so I noticed that the item_id is in the order table.
It is different to distinguish the id and item_id of the product indiscriminately, so if item_id exists in the order table, it seems that it will work if you can distinguish the item_id existing on the top page and the item_id in the order table. After thinking about it, I wanted to compare the product id and item_id on the top page with the item_id on the order table, so I arrived at the exists? Method.
When I wrote it in sentences, I wrote it in order, but when I was thinking about it, I first thought that it might be compared with item_id in the order table, and I thought back and came up with the answer.
When thinking about the desired behavior in programming, I realized that it is important to think about the process in detail for the abstracted purpose.
Recommended Posts