I want to make a app similar to www.imdb.com, now I have a problem to define the association of two table --people and movies . Movie stars, movie writers... are allow people, I plan to put all people data into one table named "people", and I''ll use "movies" table to store movies'' information. A movie may have none or more than one movie writer/ movie star information, and a movie star may show up on several movies. 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 :personMovie Class>has_many :movies_peoplePerson Class>has_many :personAdd methods to Movie.rb, like:>def movie_stars > Person.find(movies_people.find_by_char_type("movie_star").collect{|o| o.person_id}) >endThe question is: is that a good idea, or there are better ones? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
How about a roles table? 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 :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nanyang Zhan wrote:> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Emmanuel Oga wrote:> How about a roles table? >> 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy Woertink wrote:> 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---