5. Movies II, Step 2: "retrieve all actors..."

I know that this step tells me to retrieve all actors related to the movie, but I can’t find a way to relate both models (is it technically right? relating models?) in order to list all instances of one class (actor) that relate to one instance of another (movie). Is this way of thinking even correct?

So far my code looks like this:

def show
@movie = Movie.find(params[:id])
@actors = @movie.actor
end

this gives me the error “undefined method `actor’ for #Movie:0x0000000627d7a0

Ok, so I kept searching and came to the solution, which was pretty close to what I had.

This worked for me:

def show
@movie = Movie.find(params[:id])
@actors = @movie.actors
end

As the said page says, this is a collection of joined models.

2 Likes

Why “.actors” is become a method for instance variable “@movies”?

@actors = @movie.actors

after “.”(dot) there is always a method. Do I understand it right?

EDIT: I understood after two weeks and few more courses.

1 Like

A post was split to a new topic: The code still does not work. any idea why?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.