Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails上分组_Ruby On Rails_Group By - Fatal编程技术网

Ruby on rails 如何在Rails上分组

Ruby on rails 如何在Rails上分组,ruby-on-rails,group-by,Ruby On Rails,Group By,我是ROR的新手,要做到这一点我有麻烦 我有一个工作时间模型和一个商户模型,其中商户有很多工作时间,工作时间属于商户,如下所示: class Merchant < ApplicationRecord has_many :working_hours, inverse_of: :merchant, dependent: :destroy accepts_nested_attributes_for :working_hours, reject_if: :all_blank, all

我是ROR的新手,要做到这一点我有麻烦

我有一个工作时间模型和一个商户模型,其中商户有很多工作时间,工作时间属于商户,如下所示:

class Merchant < ApplicationRecord 
  has_many   :working_hours, inverse_of: :merchant, dependent: :destroy
  accepts_nested_attributes_for :working_hours, reject_if: :all_blank, allow_destroy: true
end
商户可以在同一天有两个工作小时

我的看法是:

<% @merchant.working_hours.order(:day).each do |wh| %>
  <li>
    <%= t(:"date.abbr_day_names")[wh.day.to_i] %> : 
    <%= wh.open_time.to_formatted_s(:time)  %> -
    <%= wh.close_time.to_formatted_s(:time)  %>
  </li>
<% end %>
如何将工作时间按天分组并以这种方式显示:

Mon: 10:00-13:00 / 17:00-20:00
Tue: 10:00-13:00 / 17:00-21:00
Wed: 10:00-13:00

我在Railscast上看了一组一组的教程,但是我没有一个控制器来控制工作时间模型。有什么想法吗

您可以将值保存在散列中,并使用它以所需格式打印

散列中的键将是
t(:“date.abbr\u day\u names”)[wh.day.to\u i]
值将是
wh.open\u time.to\u formatted\u(:time)-wh.close\u time.to\u formatted\u(:time)

结束

然后可以使用@merchant\u working\u hours散列

<% @merchant_working_hours.each do |key,value| %>
<%= key: value%>
<% end %>


若要以所需格式打印值,您可以使用“创建于”和“更新于”值或“打开时间”和“关闭时间”来检查是否有超过一周的数据

您可以将值保存在散列中,并使用它以所需格式打印

散列中的键将是
t(:“date.abbr\u day\u names”)[wh.day.to\u i]
值将是
wh.open\u time.to\u formatted\u(:time)-wh.close\u time.to\u formatted\u(:time)

结束

然后可以使用@merchant\u working\u hours散列

<% @merchant_working_hours.each do |key,value| %>
<%= key: value%>
<% end %>


若要以所需格式打印值,您可以使用“创建于”和“更新于”值或“打开时间”和“关闭时间”来检查是否有超过一周的数据

您可以使用ruby Enumerator,它将为您提供具有该结构的新集合

{ 
  key: matching collection items (minus item grouped on), 
  key2: matching collection items (minus item grouped on)
}
这意味着您的.each now将应用于一个散列结构,其中键变为整数或您按天分组的对象,在本例中,它也恰好是整数,以及一个子集合,该子集合匹配但已从中删除您按天分组的属性。所以在你的情况下,你可以重写

<% @merchant.working_hours.order(:day).each do |wh| %>
  <li>
    <%= t(:"date.abbr_day_names")[wh.day.to_i] %> : 
    <%= wh.open_time.to_formatted_s(:time)  %> -
    <%= wh.close_time.to_formatted_s(:time)  %>
  </li>
<% end %>

  • : -
  • 作为

    
    : 
    
  • -
  • 0 %> /
    请注意,这意味着所有分组都在ruby层上进行,这将导致大型数据集出现问题,而且您现在在ruby层中,因此您将失去AREL延迟加载功能,例如,排序必须在ruby now中完成,而不是对“子集合”应用新的SQL顺序。理想情况下,您可能希望执行SQL分组,但这是另一个时间


    注意,我没有测试这段代码,所以它可能会爆炸,因为是。。。YMMV,我相信你必须重新格式化一下

    您可以使用ruby Enumerator,它将为您提供具有该结构的新集合

    { 
      key: matching collection items (minus item grouped on), 
      key2: matching collection items (minus item grouped on)
    }
    
    这意味着您的.each now将应用于一个散列结构,其中键变为整数或您按天分组的对象,在本例中,它也恰好是整数,以及一个子集合,该子集合匹配但已从中删除您按天分组的属性。所以在你的情况下,你可以重写

    <% @merchant.working_hours.order(:day).each do |wh| %>
      <li>
        <%= t(:"date.abbr_day_names")[wh.day.to_i] %> : 
        <%= wh.open_time.to_formatted_s(:time)  %> -
        <%= wh.close_time.to_formatted_s(:time)  %>
      </li>
    <% end %>
    
    
    
  • : -
  • 作为

    
    : 
    
  • -
  • 0 %> /
    请注意,这意味着所有分组都在ruby层上进行,这将导致大型数据集出现问题,而且您现在在ruby层中,因此您将失去AREL延迟加载功能,例如,排序必须在ruby now中完成,而不是对“子集合”应用新的SQL顺序。理想情况下,您可能希望执行SQL分组,但这是另一个时间


    注意,我没有测试这段代码,所以它可能会爆炸,因为是。。。YMMV,我相信你必须重新格式化一下

    看起来您已经在视图中获得了所需的所有数据。你只需要适当地操纵它。也许从你的角度发布代码?我编辑了我的问题。Ben,这么做简单吗?看起来你已经在视图中获得了所需的所有数据。你只需要适当地操纵它。也许从你的角度发布代码?我编辑了我的问题。这么做简单吗Ben?谢谢。就目前而言,这是可行的您是否建议新手了解SQL group的资料?这是一篇好的开始文章?是的,主要是谷歌fu或你当地的图书馆。SQL在核心方面变化不大,所以库中的大部分内容都应该是相关的。如果你使用MySQL并且想要速度,这是我刚开始的时候推荐的,谢谢。就目前而言,这是可行的您是否建议新手了解SQL group的资料?这是一篇好的开始文章?是的,主要是谷歌fu或你当地的图书馆。SQL在核心方面变化不大,所以库中的大部分内容都应该是相关的。如果你使用MySQL并且想要速度,这是我刚开始的时候推荐的,
    <% @merchant.working_hours.order(:day).group_by(&:day).each do |day, whs| %>
      <%= t(:"date.abbr_day_names")[day.to_i] %> : 
      <%= whs.each_with_index do |wh, index| %>
        <li>
          <%= wh.open_time.to_formatted_s(:time)  %> -
          <%= wh.close_time.to_formatted_s(:time)  %>
        </li>
        <%# if we have more than one entry add slash %>
        <%= if index > 0 %>
          /
        <% end %>
      <% end %>
    <% end %>