The aim: the user logs in, and sees a list of the next-airing episodes of the TV shows they're subscribed to. So there are three regular tables (Users, TvShows, Episodes) and one join table (Subscriptions, which joins User and TvShow).
The models:
- TvShow
has_many :users, through: :subscriptionshas_many :episodeshas_many :subscriptions
- Episode,
belongs_to :tv_show - User
has_many :tv_shows, through: :subscriptionshas_many :subscriptions
- Subscription
belongs_to :tv_showbelongs_to :user
To get a list of shows a user is subscribed to, I can say current_user.subscriptions(includes: :tv_show).all.
Here's where I get stuck, though: I want to select the current user's subscriptions, including the TV shows, and including the episodes of that show.
If possible, even more specifically, I'd like to include only one episode: the episode with the lowest possible airtime value (airtime is epoch time) > Time.now.to_i (ie, the episode that airs next).
I've tried searching for this in the documentation and on Google, but it's a hard question for me to describe in 30 words or less, so I haven't had much luck solving it. I would love some help here.