我在我的数据库中有这些。每个项目都属于某个类别,我想显示所订购类别中的所有项目以及类别ID。像这样:
1
Expresso
Americano
2
Capuchino
这是我的种子代码:
{content: "expresso", price: "29000", category_id: 1},
{content: "Americano", price: "29000", category_id: 1},
{content: "Capuccino", price: "35000", category_id: 2},
这是执行查询的控制器操作:
def menu
@items = Item.order('content ASC')
end
答案 0 :(得分:0)
在您看来:
<%- Category.includes(:items).order('categories.id').each do |category| %>
<%= category.id %>
<%- category.items.each do |item| %>
<% = item.content %>