I have a movie app that's comprised of the following models: Actor
, Movies
, Genre
. I've created a many-to-many association where actors have and belong to many movies, vice versa.
class Actor < ActiveRecord::Base
has_many :movies, through: :actor_movies
has_many :actor_movies
end
class ActorMovie < ActiveRecord::Base
belongs_to :actor
belongs_to :movie
end
class Movie < ActiveRecord::Base
belongs_to :genre
has_many :actors, through: :actor_movies
has_many :actor_movies
end
I also made it so that each movie has their own genre:
class Genre < ActiveRecord::Base
has_many :movies
end
On the actor's show
page I'd like to display the 3 most common genres of movie's they've starred in. This would obviously be based off their movie genres. What I'm trying to figure out is how to put these movies (with their associated genres) in some sort've array and performing an action to produce the prior mentioned result.
Controller
def show
@actor = Actor.find(params[:id]
@movies = @actor.movies
end
So for instance I have an actor that's been in movies with genres ranging from Action, Comedy, Drama, Thriller, Sci-Fi, etc. While doing some research I found the Rubular way to create a hash from arrays using inject
from the second answer of this post.
Would I do something similar in this rails project?