Query through has many relationship

I have 2 tables

  1. AI.GateReportSubclass (embeddings)
  2. AI.GateReportEvent (deliveries)

AI.GateReportEvent belongs_to AI.GateReportSubclass as

belongs_to(:embedding_in, GateReportSubclass, foreign_key: :embedding_id_in)
belongs_to(:embedding_out, GateReportSubclass, foreign_key: :embedding_id_out)

and AI.GateReportSubclass has_many AI.GateReportEvent as

has_many(:in_deliveries, GateReportEvent, foreign_key: :embedding_id_in)
has_many(:out_deliveries, GateReportEvent, foreign_key: :embedding_id_out)

Now.

Single AI.GateReportSubclass on preloading gives multiple GateReportEvent such as

  • AI.GateReportSubclass
    • in_deliveries
      • GateReportEvent
      • GateReportEvent
    • out_deliveries
      • GateReportEvent
      • GateReportEvent

each GateReportEvent has 2 dates.

  1. arrived_at
  2. left_at

I want to query those dates to get the AI.GateReportSubclass such as if the given date will fulfill the where clause on GateReportEvent's dates will be the result.

I am struggling with what join should I use to query the arrived_at and left_at dates or Is it even possible to get an AI.GateReportSubclass while filtering multiple AI.GateReportEvents, any help would be great. thanks.