2016-09-30 40 views
5

我是ROR和Postgre的新手,我很難做到這一點。Group_by - Ruby/Rails + Postgres

我有一個Working_hour模型和一個商家模型,其中商家has_many working_hours和working_hour屬於Merchant。商人可以在同一天有兩個或更多的working_hours。

我的觀點:

<% @merchant.working_hours.order(:day).group_by(&:day).each do |dia, whs| %> 
    <%= t(:"date.abbr_day_names")[dia.to_i] %> : 
    <% whs.each do |wh| %> 
     <li> 
     <%= wh.oppening_hour.to_formatted_s(:time) %> - 
     <%= wh.close_hour.to_formatted_s(:time) %> 
     </li> 
    <% end %> 
    <% end %> 

當我在一天下令視圖中顯示檢索到的數據(注意,開放時間是無序的):

Mon: 
17:00-20:00 
10:00-13:00 
Tue: 
18:00-21:00 
10:00-13:00 

我想組由星期幾和訂購日期星期幾和第二個開放時間

Mon: 
10:00-13:00 
17:00-20:00 
Tue: 
10:00-13:00 
18:00-21:00 

但正如您所看到的,目前,我正在使用紅寶石層來執行那會帶來性能問題的問題。如何使用數據庫層來實現這一點?

+0

嘗試搜索 - Postgres的按周 - 我有同樣的問題與MySQL和閱讀一些文章,然後經過了解了更多的SQL的,我做到了,那麼在它的軌道上一流水平只是自行選擇方法... –

+0

你有沒有任何'default_scope'這與命令搞亂? – Pavan

+0

沒有@Pavan。無default_scope。 – user1301037

回答

2

快速Postgres的例子,如果你願意存儲在數據庫表中的數據(上隨機生成的數據集):可能出現得心應手

-- The query: 
SELECT  to_char(mytime, 'day') as weekday,    -- example to get weekday name 
      extract(dow from mytime) as weekday_num,   -- example to get weekday number 
      format(           -- format the way example output was given 
       '%s - %s', 
       date_trunc('hour', opening_time)::time(0), -- get opening hour (without milliseconds) 
       date_trunc('hour', closing_time)::time(0)  -- get closing hour (without milliseconds) 
      ) as working_hours 
FROM  mytable 
GROUP BY mytime,   -- to secure accurate ordering by timestamp 
      weekday,   
      working_hours 
ORDER BY mytime, 
      working_hours; 

-- Result: 
    weekday | weekday_num | working_hours 
-----------+-------------+--------------------- 
monday |   1 | 08:00:00 - 17:00:00 
tuesday |   2 | 08:00:00 - 16:00:00 
tuesday |   2 | 08:00:00 - 17:00:00 
wednesday |   3 | 08:00:00 - 12:00:00 
thursday |   4 | 08:00:00 - 12:00:00 
thursday |   4 | 08:00:00 - 16:00:00 
friday |   5 | 08:00:00 - 15:00:00 
friday |   5 | 08:00:00 - 18:00:00 

Postgres的文檔鏈接:

https://www.postgresql.org/docs/current/static/functions-datetime.html https://www.postgresql.org/docs/current/static/functions-formatting.html https://www.postgresql.org/docs/current/static/functions-string.html#FUNCTIONS-STRING-FORMAT

PS希望給出一些想法如何在數據庫中解決它。

+0

我如何在rails上調用它?使用範圍? 你可以給我一個例子如何將這個查詢抽象爲rails嗎? – user1301037

+0

@ user1301037 - 我不擅長Ruby,但沒有使用Activerecord來執行SQL幫助,就像這裏給出的例子一樣:http://stackoverflow.com/questions/14824453/rails-raw-sql-例如? –

2

工作時間應該按照opening_hour的順序排列,因爲您將在UI中以升序顯示開放時間。一旦訂購了工作小時,結果可以按天分組。

<% @merchant.working_hours.order(:opening_hour).group_by(&:day).each do |dia, whs| %> <%= t(:"date.abbr_day_names")[dia.to_i] %> : <% whs.each do |wh| %> <li> <%= wh.opening_hour.to_formatted_s(:time) %> - <%= wh.close_hour.to_formatted_s(:time) %> </li> <% end %> <% end %>

+0

你應該解釋這個答案如何回答這個問題。 – Smar

2
<% day = nil %> 
<% @merchant.working_hours.order(:day, :oppening_hour).each do |wh| %> 
    <% if wh.day != day %> 
    <% day = wh.day %> 
    <%= t(:"date.abbr_day_names")[day.to_i] %> : 
    <% end %> 
    <li> 
    <%= wh.oppening_hour.to_formatted_s(:time) %> - 
    <%= wh.close_hour.to_formatted_s(:time) %> 
    </li> 
<% end %> 
0

爲什麼你就不能由兩個字段排序:按 「天」 和 「opening_hour」?

<% @merchant.working_hours.order(:day).order(:oppening_hour).group_by(&:day).each do |dia, whs| %> 
    <%= t(:"date.abbr_day_names")[dia.to_i] %> : 
    <% whs.each do |wh| %> 
     <li> 
     <%= wh.oppening_hour.to_formatted_s(:time) %> - 
     <%= wh.close_hour.to_formatted_s(:time) %> 
     </li> 
    <% end %> 
    <% end %>