0

I am trying to implement a check to see if two opening_hours are overlapping with each other upon create. When you create a store, you can add only one opening_hour for Monday like 10:00 until 20:00, or two like Monday 10:00 - 13:00 and Monday 14:00-20:00. In the last case, it creates two entries with the same day, which is fine but I need to check if the opening_hours are overlapping.

I have a Store and OpeningHour model :

class Store < ApplicationRecord
  has_many :opening_hours, dependent: :destroy
  validates :name, presence: true
  accepts_nested_attributes_for :opening_hours, reject_if: ->(opening_hours) { opening_hours[:day].blank? && opening_hours[:open].blank? },
                                             allow_destroy: true
end
class OpeningHour < ApplicationRecord
  belongs_to :store
  validates :day, acceptance: { accept: Date::DAYNAMES }, allow_blank: true
end

And here is the schema:

  create_table "opening_hours", force: :cascade do |t|
    t.bigint "store_id", null: false
    t.string "day"
    t.time "open"
    t.time "close"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["store_id"], name: "index_opening_hours_on_store_id"
  end

  create_table "stores", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

I tried to use the validate overlap gem, but it was not working like I wanted :

validates :open, :close, overlap: { exclude_edges: %w[open close], scope: :store_id }

The problem with the gem is that I need to scope with the store_id I am currently creating, and also to check if there are two opening_hours created for Monday for example, so I need to scope with the day. However when I scope with the store_id nothing is happening, but when I scope with the day, I cannot create anything.

I tried to create a custom scope to add in query_options, but it didn't work either... And all the solution I checked, I cannot make them work. Do I need to scope something else? Or to not use the gem at all? Thanks for the help!

thibc
  • 1
  • Why do you need a gem for this? Add a simple [custom validation](https://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations) that does _only what you need_ without any additional overhead (including mental one) ... This is what I'd do in the 1st place. – Konstantin Strukov Oct 19 '22 at 15:40
  • Does this help? https://stackoverflow.com/questions/74070757/how-to-validate-range-dates-with-rails-6/74071005?noredirect=1#comment130835783_74071005 – engineersmnky Oct 19 '22 at 16:14
  • @KonstantinStrukov I tried to do a custom validation but it was not working like I wanted so I thought with a gem it might be easier. – thibc Oct 19 '22 at 17:17
  • But the post @engineersmnky sent gave me an idea on maybe what was not working, so I'll try that, thanks! – thibc Oct 19 '22 at 17:17
  • It's done thanks both of you! I made a custom validation and now it's all good! – thibc Oct 19 '22 at 18:57

0 Answers0