Question

i want to define a many-to-many relation in a rails project. how is the best way to give the individual relations different meanings?

+------------+        has many        +-------------+
|            | ---------------------> |             |
|   person   |                        |   project   |
|            | <--------------------- |             |
+------------+        has many        +-------------+

this model is good for a start, but not enough for what i want to achieve. a person should be able to play different roles in a project. e.g. in a movie there are actors, producers, special effects guys, ...

the solution should...

  • provide an easy way to define new types of relations ('roles')
  • integrate into rails in a nice way
  • be as fast as possible

what are the best options?

Was it helpful?

Solution

The best way is to handle this is to create a rich join table.

Ie:

       |  has many =>  |        |  <= has many  |
Person |               | Credit |               | Movie
       | <= belongs to |        | belongs to => |

Where Person and Movie don't change much from the initial example. And Credit contains more fields than just person_id, and movie_id. The extra fields for Credit would be role, and character.

Then it's just a has many through relationship. However we can add extra associations to get more detail. Keeping with the movies example:

class Person < ActiveRecord::Base
  has_many :credits
  has_many :movies, :through => :credits, :unique => true
  has_many :acting_credits, :class_name => "Credit",
    :condition => "role = 'Actor'"
  has_many :acting_projects, :class_name => "Movie",
    :through => :acting_credits
  has_many :writing_credits, :class_name => "Credit", 
    :condition => "role = 'Writer'"
  has_many :writing_projects, :class_name => "Movie",
    :through => :writing_credits

end 

class Credit < ActiveRecord::Base
  belongs_to :person
  belongs_to :movie
end

class Movie < ActiveRecord::Base
  has_many :credits
  has_many :people, :through => :credits, :unique => true
  has_many :acting_credits, :class_name => "Credit",
   :condition => "role = 'Actor'"
  has_many :actors, :class_name => "Person", :through => :acting_credits
  has_many :writing_credits, :class_name => "Credit", 
   :condition => "role = 'Writer'"
  has_many :writers, :class_name => "Person", :through => :writing_credits
end

With all those extra associations. Each of the following is only one SQL query:

@movie.actors  # => People who acted in @movie
@movie.writers # => People who wrote the script for @movie

@person.movies # => All Movies @person was involved with
@person.acting_projects # => All Movies that @person acted in

OTHER TIPS

The relationship between people, projects and roles should be a table of its own. Make an Assignment class that has a person, a project and the person's role in that project. Then Person has_many :projects, :through => :assignments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top