Real talk. HABTM means HasAndBelongsToMany, but it’s one of those confusing acronyms that doesn’t really match what it stands for.
See, when you Wiki it, the page that comes up is Many-To-Many. Which is fine, it makes sense, but it’s not matching. I kinda like when things match, ya know?
When you’ve been programming a while, you get used to hacking together HABTM or many-to-many commands. These two rays of sunshine are basically a way of modelling data, or showing a correlation between database tables.
Database tables are built around cardinality, which is essentially a very precise description of why those tables should be linked, and the logic that keeps them linking together accurately.
Now, many-to-many tabling is pretty fancy, but to to make it work you also need to understand one-to-many tabling. To stick with the excellent example given in Wikipedia on cardinality (this table association):
A hospital database might contain 3 different ‘tables’ or data sets that need to be linked together: the table of doctors working there, the patients being treated, and the departments that make up the hospital.
In a one-to-many situation, the doctors and departments might be linked, as each doctor would only operate in one department (so for example, you’re not going to find the oncologists showing up in the pediatrics table).
But in a many-to-many situation, you might find the crossover between multiple patients being treated by multiple doctors – and sometimes even in multiple departments.
This crossover is where understanding HABTM really counts. You can’t be having these table interactions get messed up or the database is going to spit out completely screwy data. Getting familiar with HABTM takes time but it’s worth putting in the effort to get it right, because it’s going to open many, many doors for you. Get your tabling accurate and you’ve got a multitude more projects you can work on.