Ruby on rails 通过字符串收集记录并生成这些记录的数组

Ruby on rails 通过字符串收集记录并生成这些记录的数组,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有一个像这样的循环: <% current_user.brand.templates.each do |template| %> <li><%= link_to(template.label, new_templated_document_path(template)) %></li> <% end %> categories = [] Template.categories.each do |c| category = [

我有一个像这样的循环:

<% current_user.brand.templates.each do |template| %>
  <li><%= link_to(template.label, new_templated_document_path(template)) %></li>
<% end %>
categories = []
Template.categories.each do |c|
  category = []
  category << Template.where(category: c).all
  categories << category
end
categories
不过感觉很笨拙。有什么帮助吗

更新

我已将其更改为:

@categories = {}
Template.first.categories.each do |c|
  @categories[c] = current_user.brand.templates.where(category: c)
end
但我似乎无法将记录放入散列。这可能吗?

根据您的需要,您可以使用类似于
Template.where(category:Template.categories)
的东西。您还可以查看以下内容:

Client.where(orders_count: [1,3,5])
将产生

SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
我想这正是您想要的。

根据您的需要,您可以使用类似于
模板的东西。其中(category:Template.categories)
。您还可以查看以下内容:

Client.where(orders_count: [1,3,5])
将产生

SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
我想这正是您想要的。

根据您的需要,您可以使用类似于
模板的东西。其中(category:Template.categories)
。您还可以查看以下内容:

Client.where(orders_count: [1,3,5])
将产生

SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
我想这正是您想要的。

根据您的需要,您可以使用类似于
模板的东西。其中(category:Template.categories)
。您还可以查看以下内容:

Client.where(orders_count: [1,3,5])
将产生

SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))

我想这正是您想要的。

我想您可以查询模板并按类别对它们进行分组,这样会更容易

Template.all.group_by(&:category)

你会得到一个散列,其中键是类别,值是模板列表

我想你可以查询模板并按类别对它们进行分组,这样会更容易

Template.all.group_by(&:category)

你会得到一个散列,其中键是类别,值是模板列表

我想你可以查询模板并按类别对它们进行分组,这样会更容易

Template.all.group_by(&:category)

你会得到一个散列,其中键是类别,值是模板列表

我想你可以查询模板并按类别对它们进行分组,这样会更容易

Template.all.group_by(&:category)

您将得到一个哈希,其中key是类别,value是模板列表,这要优雅得多。谢谢你的提示!那要优雅得多。谢谢你的提示!那要优雅得多。谢谢你的提示!那要优雅得多。谢谢你的提示<代码>分组依据!完美的非常感谢。
分组人
!完美的非常感谢。
分组人
!完美的非常感谢。
分组人
!完美的非常感谢。