habtm vs has_many :through

Of late the ActiveRecord association code has been getting a lot of love. One of the high profile additions are polymorphic associations which turned into one of the big features of the 1.1 release. Amongst all the commotion, some may not have noticed that there is an enhanced way to do many to many associations other than with has_and_belongs_to_many.

For some time people have been skipping out on has_and_belongs_to_many in favor of setting up two has_many associations so that they get the benefits of a full join model and what that brings with it. With 1.1’s new :through association option, a two way has_many just got even sweeter. So if there are two ways to do a many to many relationship, what are the differences, and which approach should you take?

Josh Susser has been digging deep into the association code lately and has emerged from the thicket with a point by point comparison of habtm and has_many :through, a dance-off if you will, between the two approaches. This would be a good time to add him to your RSS reader if you haven’t already.

blog comments powered by Disqus