我有三种型号:
Channel
> Program
> Episode
Channel has_many programs
Program belongs_to channel
Program has_many episodes
Episode belongs_to program
如何进行此查询?
*“Episode
个Channel
的{{1}}总Program
个数。*
更详细一点,
我真的被困了。
如何最有效地执行此操作,而不是编写代码行和许多查询?
答案 0 :(得分:1)
我不知道如何处理活动记录。但你可以用sql解决这个问题,
例如
select top 1 c.channel_id, count(p.) as count from Program as p inner join Channels as c on c.channel_id=p.channel_id group by c.channel_id order by 2 desc