简体   繁体   English

Ruby / Rails-排序和显示大量关联数据

[英]Ruby/Rails - Sorting and Presenting a Large Amount of associated data

I have a model that have a column for name and a column for categories. 我有一个模型,其中有一个名称列和一个类别列。 There are a large amount of names that I would like to list by category but I haven't figured out how to do it. 我想按类别列出大量名称,但我还没有弄清楚该如何做。

Currently in the view I have 目前在我看来

<% for car in @cars %>
<%= car.name %>
<% end %>

which just presents this huge list of that is way too unwieldy. 只是呈现了如此庞大的清单太笨拙了。 I'm using @car = Car.find(:all) in the controller to get the selection. 我在控制器中使用@car = Car.find(:all)来获取选择。

=> If there a way I can create some form of dynamic table where it groups all the car records by category and makes a new column for each category instance, with a listing of all the associated cars? =>如果有一种方法,我可以创建某种形式的动态表,在其中按类别对所有汽车记录进行分组,并为每个类别实例创建一个新列,并列出所有相关的汽车?

=> I'm also afraid that that might be too many columns so after 5 or so columns can I start a new row? =>我还担心那可能是太多列,所以在5列左右之后我可以开始新的一行吗?

=> Should I do all this in the controller or the view? =>我应该在控制器或视图中执行所有这些操作吗?

Can you specify some sample values for the category column? 您可以为类别列指定一些样本值吗?

Depending on what you have stored you may be able to do this: 根据您存储的内容,您可以执行以下操作:

Car.find(:all).group_by(&:category) Car.find(:all).group_by(&:category)

Which will put the cars into an ordered hash indexed by the different category values. 这会将汽车放入按不同类别值索引的有序哈希。

<% @car_categories.sort.each do |category, cars| %>
    <h3><%= category %></h3>
    <% for cart in cars %>
       <p><%= interest.name %></p>
    <% end %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM