The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
A many-to-many relationship

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
David Heinemeier Hansson

Posts: 512
Nickname: dhh
Registered: Mar, 2004

David Heinemeier Hansson is the lead Ruby developer on 37signal's Basecamp and constructor of Rails
A many-to-many relationship Posted: May 11, 2004 3:12 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by David Heinemeier Hansson.
Original Post: A many-to-many relationship
Feed Title: Loud Thinking
Feed URL: http://feeds.feedburner.com/LoudThinking
Feed Description: All about the full-stack, web-framework Rails for Ruby and on putting it to good effect with Basecamp
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by David Heinemeier Hansson
Latest Posts From Loud Thinking

Advertisement

Active Record is doing a lot of magic to simplify associations between objects and their records in the database. Ruby is making this particularly easy due to ability of running methods during class definitions, which makes for instant domain specific languages.

Consider this:


class Project < ActiveRecord::Base
  belongs_to :portfolio
  has_one    :project_manager
  has_many   :milestones
end

Our project objects are now able to respond to methods such as project.portfolio, project.has_project_manager?, and project.create_in_milestones("deadline" => Date.today + 5). This massively simplifies the wiring of the domain model and makes it much easier to traverse the graph when you get hold of just one of the ends.

I'm also particularly fond of the naming scheme, which makes reading aloud a natural task. "Project belongs to Portfolio", "Project has one Project Manager", and "Project has many Milestones". Great.

But how do we represent many-to-many relationships? Currently, it has the slightly clumsy macro of has_and_belongs_to_many, as illustrated below:


class Project < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

This makes it possible to do things like project.add_categories(critical, technical) that'll create relationships with the critical and technical category objects through a join tabel. It's also fairly readable as you'd say "Project has and belongs to many Categories".

But, it's also a little clumsy. So here's a challenge: By which other name may the m-m association macro in Active Record smell as sweet? The rules are pretty simple:

  • It has to read aloud with natural ease
  • It has to follow the form of has_one, has_many, and belongs_to
  • It has to be less clumsy than the current naming

There's an honorable mentioning in the Active Record README at stake to anyone who can crack this (priceless tribute, really!).

Read: A many-to-many relationship

Topic: Ant Talk At CJUG Previous Topic   Next Topic Topic: The Building of Basecamp is a reality

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use