I want to make association between people table and movies, in such a
way that I can list all movie stars who shown on one movie with
movie.movie_stars, and writers with movie.writers, while
movie.movie_writers << person will add a movie writer info.
Here's my current idea:
A third table,named "movies_people", containing movie_id, person_id,
char_type columns, is to be added. char_type column is used to store
person's role in the movie, like "movie_star" to identify the person is
added as a movie star.
MoviesPeople Class
>belongs_to :movie
>belongs_to :person
Movie Class
>has_many :movies_people
Person Class
>has_many :person
Add methods to Movie.rb, like:
>def movie_stars
> Person.find(movies_people.find_by_char_type("movie_star").collect{|o| o.person_id})
>end
The question is: is that a good idea, or there are better ones?
--
Posted via http://www.ruby-forum.com/.
Class Rol < AR::Base
has_many :participations
has_many :people, :through => :participations
end
Class Person < AR::Base
has_many :participations
has_many :movies, :through => :participations
has_many :roles, :through => :participations
end
Class Participation < AR::Base
belongs_to :rol
belongs_to :person
belongs_to :movie
end
Class Movie < AR::Base
has_many :participations
has_many :persons, :through => :participations
end
ie
Person.participations => may bring an array of participation objects,
like
[[person, movie, rol], ...] => [["John", "A Movie", "Actor"], ["John",
"Another Movie", "Director"], ...]
Person.movies returns all the movies of the person.
Rol.find_by_name("Actors").people => Return all the persons that have
record on partipation in the role of actor.
THE PROBLEM: With this system you can't assign a person a rol if it has
not acted in a movie sometime, ie: you can't know if a person is an
actor until he actually acts in a movie!. You could add another table to
state the people => rol relationship, even if they have not participated
yet on a movie:
make a table people_roles => person_id, rol_id, whitout an ActiveRecord
class, and add add:
has_and_belongs_to_many :roles to class People and
has_and_belongs_to_many :people to class Rol
YES! it's a messy response to a messy problem :)
> Here's my current idea:
> A third table,named "movies_people", containing movie_id, person_id,
> char_type columns, is to be added. char_type column is used to store
> person's role in the movie, like "movie_star" to identify the person is
> added as a movie star.
>
> MoviesPeople Class
>>belongs_to :movie
>>belongs_to :person
>
> Movie Class
>>has_many :movies_people
>
> Person Class
>>has_many :person
>
>
> Add methods to Movie.rb, like:
>>def movie_stars
>> Person.find(movies_people.find_by_char_type("movie_star").collect{|o| o.person_id})
>>end
>
> The question is: is that a good idea, or there are better ones?
A movie has many people and a person belongs to a movie, or you could do
a HABTM association so a movie has and belongs to many people and a
person has and belongs to many movies.
~Jeremy
> Class Participation < AR::Base
> belongs_to :rol
> belongs_to :person
> belongs_to :movie
> end
>
A participation model! It sounds attractive.With this model, the
relation of people and movies is clearer.
> ie
> Person.participations => may bring an array of participation objects,
> like
>
> [[person, movie, rol], ...] => [["John", "A Movie", "Actor"], ["John",
> "Another Movie", "Director"], ...]
>
> Person.movies returns all the movies of the person.
Good. But it seems all the favors go to the people part. How to make
movie.actors show all the actors of one movie?
> THE PROBLEM: With this system you can't assign a person a rol if it has
> not acted in a movie sometime, ie: you can't know if a person is an
> actor until he actually acts in a movie!.
Then, just let it be. Maybe people wouldn't notice an actor without an
movie.
> make a table people_roles => person_id, rol_id, whitout an ActiveRecord
> class, and add add:
>
> has_and_belongs_to_many :roles to class People and
> has_and_belongs_to_many :people to class Rol
...All sounds good, except one thing:I came here looking for a simple
solution.
> a HABTM association so a movie has and belongs to many people and a
> person has and belongs to many movies.
A habtm association can not tell the role of one person that
participated for the movie.