A A
RSS

Has and belongs to many

Mon, Dec 4, 2006

Programming

The has_many_and_belongs_to_many relation is a very powerful construct in Ruby to set a relation between two entities. You can use this for many to many relations. For instance the relation between a person and a project.
A person works in many projects, and a project can have many persons working on it. Typically this will lead to three tables:

  • Person table (id, name, etc.)
  • Project table (id, name, etc)
  • Person_project table (person_id, project_id) storing the relation between a project and a person

In Ruby on rails you will have two classes in your model:

RUBY:
  1. class Person <ActiveRecord::Base
  2.    has_and_belongs_to_many :project
  3. end
  4.  
  5. class Project <ActiveRecord::Base
  6.    has_and_belongs_to_many :person
  7. end

By creating these relations rails creates some properties for you. One of the properties created in the Person class is the property project. You can do the following

RUBY:
  1. person = Person.find (1) #Find person with id 1
  2. #Render the name of the first project the person is in
  3. render_text person.project[0].name

This is all great. But what if there are multiple relations between person and project. One relation between project and person may be that a certain person works in a project. Another relation could be that a number of persons fund a project. We need to enhance the relation between Person and project.

RUBY:
  1. class Project <ActiveRecord::Base
  2.    has_and_belongs_to_many :fund,
  3.       :class_name => "Person",
  4.       :join_table => "project_fund"
  5.    
  6.    has_and_belongs_to_many :employee,
  7.       :class_name => "Person",
  8.       :join_table => "project_emp"
  9.  
  10. end

A project now has funders and employees. The relations are stored in the database tables project_fund and project_emp. The table project_person is no longer needed.

RUBY:
  1. project = Project.find (1) #Find person with id 1
  2. #Render the name of the first person that funds this project
  3.  
  4. text = "First employee " +
  5.    project.employee[0].name  +
  6.    " first funder " +
  7.    project.fund[0].name
  8.  
  9. render_text text

More documentation can be found on link

Popularity: 37% [?]

4 Comments For This Post

  1. Niels Bruin Says:

    I posted 18 hours sooner with this http://www.nielsbruin.nl/blog/?p=73. :)

  2. Rémon Strampel Says:

    Hi Niels, that’s true. But you didn’t described in detail how to create multiple relations between the same tables. This was a problem arno and i encountered.

  3. Niels Bruin Says:

    I didn’t said it was the same, just coincidence thats these object come around now. My intention was to give some extra information.

  4. Niels Bruin Says:

    object = subjects, to much as3 :)

1 Trackbacks For This Post

  1. Blast from the past | Web X.Y Says:

    [...] Ruby on rails can manage relations in many on many relations (has_many_and_belongs_to_many). Read more [...]