Engineering archive · originally published 2022
Has and Belongs To Many (Headaches)
Tarun Mookhey
CTO / Principal Engineer, Pacifica Labs
The HABTM association is often confusing and can lead to frustration if defined incorrectly. Here find instructions on how to set it correctly.
Why this matters:
One of the more complicated associations…..
ActiveRecord requires you to *define the relationships between two classes. Whilst the has_many and belongs_to association methods attract the most attention*, let’s consider the more obscure has_and_belongs_to_many.
Appendix
BTW Did anyone tell you that software development requires you to spend an insane amount of time thinking of the nature of things in the world and their interrelationships? All in the name of data modelling.
And let’s admit, especially for beginners, newbies for those rusty on ActiveRecord Associations, we’ll be tempted to try and jury-rig as many relationships into this association and make it work.
More from the Blog
Engineering
How to modernise a legacy Rails application without rewriting the business
A rewrite replaces the risky parts and the valuable parts at the same time. Staged modernisation lets you separate them.