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
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!).