New many-to-many relationships in Rails

This post on a Smalltalk blog caught my attention. So, I headed over to the link and listened to the podcast. Mind you, I do subscribe to the Ruby on Rails podcast but have not been religiously listening to each episode. Maybe I will from now on.

Anyway, something in that particular podcast piqued my interest. No, it wasn't the part about how similar Ruby is to Smalltalk. I came to that conclusion myself when learning Smalltalk after learning and using Ruby for some time. More interesting was the mention of a new construct for mapping many-to-many relationships. The venerable has_and_belongs_to_many now has a new sibling, appropriately called has_many :through.

I was running into some problems using has_and_belongs_to_many for my grading application. I wanted to join two tables using a relationship and I wanted to assign attributes on that relationship as well. It's kind of like the normal E/R diagram that you have showing a relationship on two entities with attributes on the relationship itself. Usually, what one would do is to create a table to represent the relationship and then add some fields to the table. Unfortunately, the previous release of Rails did not have nice support for adding attributes to the relationship. Refer to column "When a Join wants to Be a Model" page 232 of Agile Web Development with Rails to see the previously suggested way of handling this scenario.

Anyway, to cut a long story short, the latest version of Rails introduces the ability to clearly represent the join table using has_many :through. No longer would you use the un-object oriented way of push_with_attributes to add attributes to the join table. This makes my life so much easier and is a nice change to have in Rails. Moreover, the second edition of the Agile Web Development with Rails book already features this method.

In the mean time, there is a nice article about all this at: Many-to-many Dance-off


comments powered by Disqus