For a while, I’ve been getting that HMT is replacing HABTM. It appears
that
HMT can do all of what HABTM can do and more. The question is: Should I
stop
using HABTM? Let’s take a simple case:
A case has many categories
For a given category, there are certain valid statuses
Category
has_and_belongs_to_many :statuses
Status
has_and_belongs_to_many :categories
Question:
Is there value associated with creating the join model to implement HMT
for
something this simple? I’m asking because it seems HABTM is on the
endangered features list.
Is there value associated with creating the join model to implement HMT for
something this simple? I’m asking because it seems HABTM is on the
endangered features list.
Thanks
I use both HABTM and Has-Many-Through (HMT) depending on what “feels”
right. In the strictly many-to-many case, such as seems to be in your
example, I see no problem with sticking with HABTM. HTM is typically
important when there is a value in having a real abstraction for the
“middle” object (from which you can hook other relations and enforce
validations).
In any case, HTM and HABTM are not mutually exclusive, and I like
having both of them around; I see no reason for eliminating HABTM (not
to mention this would break compatibility with older Rails apps in a
pretty serious way). I don’t think HABTM is “endagered.”
HMT isn’t appropriate in all cases. There are many times when I want
a relationship between two objects, but the association has usefulness
on its own. I would be unhappy if they got rid of HABTM and
handcuffed me to create a meaningless association models there there
is none needed.
As would I… and I’m also not happy that push_with_attributes has
been deprecated; It’s not a good practice to use
push_with_attributes, but it sure makes it easy to work with a legacy
database where creating a new table just won’t work for you.