3

I have three models: Channel > Program > Episode

Channel has_many programs
Program belongs_to channel
Program has_many episodes
Episode belongs_to program

How to make this query?

* "Number of total Episodes of associated Channel which has the highest number of Programs. *

To be more detailed,

  1. Find the Channel which has the highest number of programs.
  2. Find number of episodes joining with programs that belong to that Channel.

I'm really stucked.

How to do this most efficiently instead of writing lines of codes and many queries?

7
  • sql server,mysql,oracle? Commented Jan 21, 2014 at 10:26
  • @Mihai active record query is independent of databases right? Commented Jan 21, 2014 at 10:30
  • @emaillenin To be frank,I had no idea what active record is. Commented Jan 21, 2014 at 10:31
  • It's postgresql @Mihai. Any help with even with SQL appreciated. Commented Jan 21, 2014 at 10:55
  • So you want to find the total count of episodes for the channel with the greatest number of programs? What treatment do you want where there are many channels that all have the greatest number of programs, but presumably different numbers of episodes? Commented Jan 21, 2014 at 12:01

1 Answer 1

1

I dont know how to do with active record. But you can solve this problem with sql,

for example

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.