Simple custom range in Ruby

I have a Rails named_scope that uses a condition to pull certain days of the week from a table as follows:

:conditions => [ 'EXTRACT(DOW FROM bookdate) IN (?)', (1..6).to_a ]

The date range 1..6 will be variable depending on the dates that the user wants,

What creates this SQL

(EXTRACT(DOW FROM bookdate) IN (1,2,3,4,5,6)

My problem is that the days of the week are not a simple range ... i.e. 1..6 works fine (Mon ... Sat), but let's say that 6..2 will not work correctly (Sat-W) ... either as a ruby โ€‹โ€‹range, or as it should be 6,7,1, 2, and not 6,5,4,3,2 (provided that 6,2 worked in a ruby, which is not).

How do I create a custom range for the days of the week that matches a date range like this?

Any ideas?

Thank,

+3
1

, :conditions, , Sat-, Mon-Sat, :

def day_range(from, to)
  if from <= to
    (from..to).to_a
  else
    (from..7).to_a + (1..to).to_a
  end
end

.

irb(main):032:0> day_range(1, 6)
=> [1, 2, 3, 4, 5, 6]
irb(main):033:0> day_range(6, 2)
=> [6, 7, 1, 2]

, .

+7

Source: https://habr.com/ru/post/1731756/


All Articles