首先,我强烈建议在你身上使用两个变量。 当你使用一个变量时,按组别将各种数值分成两个变量。 这比将奶制品指数与小组的3.1比更为明确。
One way is that the first part of the pair will be the id, so you can do a find.
<% @products.group_by(&:product_group_id).each do |product_group_id, products|%>
<!--need to get group name here -->
<%= ProductGroup.find(product_group_id).name %>
<% products.each do |product|%>
<%= product.product_name%>
<br>
<% end %>
<% end %>
Another way, is since you have an array of at least on product in the group, you can call the product_group association on the first element of the array.
<% @products.group_by(&:product_group_id).each do |product_group_id, products|%>
<!--need to get group name here -->
<%= products[0].product_group.name %>
<% products.each do |product|%>
<%= product.product_name%>
<br>
<% end %>
<% end %>
你们也可以把名称交给产品组。
class Product
belongs_to :product_group
delegate :name, :to => :product_group, :prefix => true, :allow_nil => true
end
<%= products[0].product_group_name %>