Help needed to construct complex query

Hi all,

I am really trying to not use a native sql statement for the

Basically, I am trying to solve the same problem described here:

However, I need to do this in a parent child relationship.

Parent object contains (Latitude, Longitude, name, ....), Child object
contains (expired_time, start_time, ....) and it is a has_many

here is my code: