I have been working on wheresmycomedian.com quite a bit today and I’m so incredibly excited (possibly too excited) about through associations in Ruby on Rails. It’s sad that I’m regurgitating an example from a really good article explaining the differences between the two many-to-many association models… but I’m going to put it into my own context for wheresmycomedian.com.
So here’s a simple scenario: I have a list of comedians that do shows at venues… how can I get meaningful associations between the objects?
Traditionally you would create a many-to-many relationship using habtm, which in turn makes a calling JOIN statement to find the relevant association.
You could do something like this:
1 |
|
The schema is simple, code is simple… but what about this… Sometimes you might have a show that has multiple comedians. Each comedian appears at specific times… and you want to be able to model that within the show. Maybe your users only want to see a particular comedian and only wants to know about the times they are appearing.
This causes a problem, as the join association is purely a SQL statement, not a meaningful association.
Enter in has_many, :through. The concept is simple, create an association model that holds the meaningful data (ex. time of appearance). Using some ruby syntax, we can still have our clean model code and associations…
1 |
|
Excellent! Now we can have meaningful data… with the Appearance holding the date of the comedians appearance…
Once we get the :uniq patch, we’ll be able to specify more distinct associations… without specifying an ugly select statement in our :through clause. Another really sweet thing about has_many is that we can have polymorphic (heterogenous) associations… nice.