Active record order for the current day

I have a problem with sorting an array by current date.

My database has a field called day

daya day of the week, for example Monday, Tuesdayetc.

I am trying to sort an index view page using current day.

I would like to do something like this in my controller,

@happies = Happy.where(id: @search.results.map(&:id))
  .page(params[:page]) 
  .where(:day => Date.today.strftime('%A').capitalize.to_s)

but instead to return only, waiting for a day Monday, I would like orderto day, where dayis current day.

I also thought about it in my view

with something like

<% @happies.sort_by(:day => Date.today.strftime('%A').capitalize.to_s).each do |happy| %>

above does not work, but I'm trying to get what I want to achieve. Any ideas on how to implement this?

Maybe there is an activeview helper?

+4
3

, :

day_order = %w(Tuesday Wednesday Thursday Friday Saturday Sunday Monday)
@happies = @happies.sort_by{|happy| day_order.index(happy.day)}

sort_by , .

, , , will_paginate. , .

( Rails). , . ( ), , , . , , .

, , , order sort_by, SQL :

@happies = Happy.where(id: @search.results.map(&:id))
    .page(params[:page])
    .order("CASE day WHEN 'Tuesday' THEN 0 " \
                    "WHEN 'Wednesday' THEN 1 " \
                    "WHEN 'Thursday' THEN 2 " \
                    "WHEN 'Friday' THEN 3 " \
                    "WHEN 'Saturday' THEN 4 " \
                    "WHEN 'Sunday' THEN 5 " \
                    "WHEN 'Monday' THEN 6 END")

SQL, , Arel, .

Edit

, , .. , . SQL - @Snarf, :

days = %w(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)
days.rotate!(days.index(Time.zone.now.strftime("%A")))
case_pieces = days.each_with_index.map do |day, i|
  "WHEN '#{day}' THEN #{i}"
end

@happies = Happy.where(id: @search.results.map(&:id))
    .page(params[:page])
    .order("CASE day #{case_pieces.join(' ')} END")

, 0 6, . modulo, :

days = %w(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)
day_offset = days.index(Time.zone.now.strftime("%A"))
@happies = Happy.where(id: @search.results.map(&:id))
    .page(params[:page])
    .order("(day - #{day_offset} + 7) % 7")
+5

.

days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

days.rotate(days.index(Time.now.strftime("%A")))

#=> ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] 
0

, @Snarf -

today = Date.current.strftime('%A') 
# => Monday
days = Date::DAYNAMES 
# => ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
today_index = days.index(today) 
# => 1
@ordered_days = days.rotate(today_index)
# => ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
@happies = Happy.where(id: @search.results)


<!-- view -->
<% @ordered_days.each do |day| %>
  <!-- in memory selection from the @happies collection, could be emtpy -->
  <% occurring_on = @happies.select { |happy| happy.day == day } %>

  <%= day %>
  <% occuring_on.each do |happy| %>
    <%= happy %>
  <% end %>
<% end %>
0

All Articles