views:

91

answers:

2

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?

+1  A: 

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.

Chuck
thanks for your answer! do you have some buzzwords for me, so that i can read more about the topic? would be great, i am still very insecure about all this.
padde
I'd recommend reading the Rails Association Guide: http://guides.rubyonrails.org/association_basics.html
Greg Campbell
+7  A: 

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
EmFi
oh man, it's all right there! rails surprises me almost every day... (well i'm quite new to it) thanks for your great answer, exactly what i was looking for!
padde
in class Movie you mean "has_many :people, :through => :credits" instead of "has_many :people, :through => :jobs" am i right?
padde
Yeah, I wrote it as jobs, then realized that credits made more sense. So I changed it, must of missed on. Sorry for the confusion.
EmFi