我有一个库存(仓库)类,为此,我生成了一个表,现在我需要按标签号和类别进行分组(我想生成一个标识每个类别的行),并在完成后进行排序。如何将此逻辑连接到我放在一起的桌子上?
我尝试将实例变量附加到逻辑本身(@tags_grouped_by_product_and_category
)上并将表嵌套在@tags_grouped_by_product_and_category.each_with_index do |(product, tag), index| %>
内,但是它仍然没有被采用-将其放在tbody
周围或table
只是重复整个集合而无需排序或分组。
#warehouse controller
@warehouse = Warehouse.includes(
tags: [product: :primary_category]).find(params[:id])
#this is pulling up data in the console, but I haven't worked out how to interact directly with :primary_category in the code; I need to be able to pull up the :name from this eventually (secondary issue)
@tags = @warehouse.tags
@tags.group_by do |tag|
tag.product || tag.product_id
end.sort_by do |product, tag|
if product.is_a? Product
product.try(:name).to_s
else
"[DELETED PRODUCT ID: #{tag.first.product_id}]"
end
end.group_by do |product, tag|
if product.is_a? Product
product.primary_category || product.category_assignments.order(created_at: :asc).try(:first).try(:primary_category)
else
nil
end
end.sort_by do |category, products|
category.try(:position) || Float::INFINITY
end
以下是表格,供参考:
<table class="table table-striped">
<thead>
<% @attributes.each do |attribute| %>
<th scope="col" class="attribute-header"><%= attribute[:label]%></th>
<% end %>
</thead>
<tbody>
# @tags_grouped_by_product_and_category was here
<% @tags.each do |tag| %>
<tr>
<td>
<%= tag.product.name %>
</td>
<td>
<%= "#" + tag.number %>
</td>
<td><%= tag.trackable_type %></td>
</tr>
<% end %>
</tbody>
</table>