Scnerio:
https://www.funtraker.com is listing movies, tv shows and games. On show page of each resource(Movie, Tv Show etc) we want to list down the related resources.
Schema:
class Movie < AR::Base
has_many :resource_genres, as: :resource
has_many :genres, through: :resource_genres
end
class ResourceGenre
belongs_to :resource, polymorphic: true
end
Now I want to get a list of related movies based on matched genre( two movies are related if both has 'comedy` genre). And these related movies need to order by max number of matched genres.
Well here is sample movies and the expected output.
#Input
Movie Genres
Movie 1: horror, comedy, action, war
Movie 2: action, thriller, crime, animation
Movie 3: comedy, war, action, thriller
Movie 4: crime, animation, action, war
#Expected output
movie1.related_movies => [ movie3, movie2 ]
movie4.related_movies => [ movie2, remaining-three-movies-in-any-order ]
movie3.related_movies => [ movie1, movie2, movie4]
Hopefully question make sense.
UPDATE: Looking for SQL only solution. I don't need to cache the results in any another table.