Within my Cart model, which has_many Subcarts and belongs_to Locations, I'm trying to create a scope query where I get all Carts that have subcarts that have a created_at time stamp that is outside a particular amount of time from the Cart.Location.active_minutes. I'm having an issue trying to find a way to use the location.active_minutes in my query. I keep getting an undefined table error.
This is basically what I have so far.
scope :inactive_cart_bucket, -> {
where('EXISTS (SELECT s.cart_id FROM cart_subcarts s WHERE (s.cart_id = cart.id) AND (? - s.created_at) / 3600 > cart.location.active_minutes)', Time.zone.now.utc)
}