I am pretty new to ROR and Postgre and i'm in trouble to achieve this.
I have a Working_hour Model and a Merchant Model, where merchant has_many working_hours and working_hour belongs to Merchant. The merchant can have two or mores working_hours for the same day.
My view:
<% @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 %>
When I display at the view ordered by day the data retrieved are (note that the opening hour are unordered):
Mon:
17:00-20:00
10:00-13:00
Tue:
18:00-21:00
10:00-13:00
I want to group by day of week and ordering first by day of week and second by opening hour :
Mon:
10:00-13:00
17:00-20:00
Tue:
10:00-13:00
18:00-21:00
But as you can see, currently, I'm using the ruby layer to do that what brings performance issues. How can achieve this using the database layer?