Im building an API in which i have a model called Studio. Studio has many movies and many series, and movies and series have many characters of their own. Also, a character can be in many movies and/or many series, and belongs to a studio. I got all the models relational tables logistics figured out by now, but what i'm trying to do right now, i can't think of a way.
So my routes are designed so i can do something like localhost:3000/api/v1/studios/1/characters -> this way i display all the studio's (studio_id: 1) characters (from all the movies and series that belong to that studio). That is fairly easy with just doing something like:
@characters = Character.where(studio_id:@studio.id)
In my CharactersController, where i have
def get_studio
@studio = Studio.find(params[:studio_id])
end
Now i am trying to design the logistics so i can do localhost:3000/api/v1/studios/1/seriees/1/characters
And only get the characters from that specific seriees. localhost:3000/api/v1/studios/1/movies/1/characters should also be valid.
I tried
def get_movie
@movie = Movie.find(params[:movie_id])
end`
and
def get_serie
@serie = Seriee.find(params[:seriee_id])
end
in my CharactersController, with before_action :get_serie and before_action :get_movie at the very start of the code, but it gives me an error because i'm never getting both a movie_id and a seriee_id, it's either one or the other, so it can't search for both at the same time.
The POST from character i will only do from localhost:3000/studios/1/characters so that's not a problem, i only want to be able to GET from localhost:3000/studios/1/movies/1/characters and localhost:3000/studios/1/seriees/1/characters
I hope i didn't ramble to much and you are able to understand me. If you think that my way of solving this problem is just not right, please tell me as i am just learning and any advice would be very valuable.