Rails console commands

For standard select, where, sort, limit queries, I write this myself, but anything more complicated, I now as the AI. I asked the AI:

write a query that shows a category and then lists the top 5 topic starters for that category and the number of topics they started

And immediately it gave back (this is a non-discourse DB):

WITH TopTopicStarters AS (
    SELECT
        c.id AS category_id,
        c.name AS category_name,
        u.name AS user_name,
        COUNT(t.id) AS num_topics_started,
        ROW_NUMBER() OVER(PARTITION BY c.id ORDER BY COUNT(t.id) DESC) AS rank
    FROM topics t
    JOIN categories c ON t.category_id = c.id
    JOIN users u ON t.user_id = u.userid
    GROUP BY c.id, u.name
)

SELECT 
    c.category_name,
    tts.user_name,
    tts.num_topics_started
FROM categories c
JOIN TopTopicStarters tts ON c.id = tts.category_id
WHERE tts.rank <= 5;

No way could I have come up with that without a lot of googling and trial and error!