I have a Merchant model. The model accepts_nested_attributes for the Working Hour model.
Merchant Model
class Merchant < ApplicationRecord
has_many :working_hours, inverse_of: :merchant, dependent: :destroy
accepts_nested_attributes_for :working_hours, reject_if: :all_blank, allow_destroy: true
end
class WorkingHour < ApplicationRecord
belongs_to :merchant
end
class CreateWorkingHours < ActiveRecord::Migration[5.0]
def change
create_table :working_hours do |t|
t.integer :day
t.time :open_time
t.time :close_time
t.references :merchant, foreign_key: true
t.timestamps
end
end
end
Update
We've should've lost the self.
part
try to add this to your merchant model
def open?
self.working_hours.any? { |wh|
(wh.open_time.hour..wh.close_time.hour).cover?(Time.current.hour)
}
end