How do I create multiple records for each day in a given quarter?

advertisements

I have Shift model.

--- !ruby/object:Shift
attributes:
  id:
  starts_at:
  ends_at:

I want to add singelton method to create shifts for each day in given quarter.

class Shift

  def self.open_quarter(number, year)
    starts_at = "08:00"
    ends_at = "08:00"

    ...
  end
end

How to implement that in best way? I want that each shifts starts_at 8.00 am and finish 8.00 am on next day.


def self.open_quarter(number, year)
 start_time = Time.new(year, number*3 - 2, 1, 8) 

  while start_time.month <= number*3 && start_time.year == year
      Shift.create{starts_at: start_time, ends_at: start_time += 24.hours}
  end
end

make sure to set the correct timezone when using Time.new. Default is current timezone (see docs). You can also use Time.utc.