I have 3 models, Player, Detail and Hero. A Player has many Details, a Detailbelongs to a Hero. Now I want to retrieve all Heroes a Player has played. I came up with this so far.
Hero.where("id IN (SELECT hero_id FROM details WHERE player_id = 1)").group("id")
How would I write a scope for it so I can also pass the Player to the scope? This is what I got so far, but it only groups the Details. I would also like to count every Heroso at the end I have x times Hero1, x times Hero2 and so on.
scope :heroes, ->(player) { where('player_id = ?', player.id).group("id") }
This scope is in the Detail model. I don't know if it is the best place, since I want it to return Heroes and not Details.