Has_and_belongs_to_many vs has_many via

Please explain the difference between has_and_belongs_to_many and has_many through a relationship. When and where to use which?

+69
ruby-on-rails
May 6 '10 at 11:50
source share
6 answers

As far as I remember, has_and_belongs_to_many gives you a simple lookup table that references your two models.

For example,

Stories can belong to many categories. Categories can have many stories.

 Categories_Stories Table story_id | category_id 

has_many :through gives you a third model that can be used to store various other pieces of information that do not belong to any of the original models.

for example

A person can subscribe to many magazines. Magazines can have many subscribers.

Thus, we can have a subscription model in the middle, which gives us a similar table for an earlier example, but with additional properties.

 Subscriptions Table person_id | magazine_id | subscription_type | subscription_length | subscription_date 

And so on.

+100
May 6 '10 at 12:30
source share
β€” -

From http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

The simplest rule is that you must configure has_many: through relationships, if you need to work with the relationship model as an independent entity. If you don’t need to do anything with the relationship model, it may be easier to set the has_and_belongs_to_many relationship (although you need to remember creating the join table in the database). You must use has_many: through if you need validations, callbacks, or additional attributes in the connection model.

+34
May 6 '10 at 3:13
source share

My rule of thumb is: can I get a list of checkboxes here? If so, then this is the habtm association. If I need a flag to learn more about communication than just yes / no, it should use has_many: through. HABTM is as simple as using the _ids method with simple_form collection_check_boxes. Has_many: through often includes accepts_nested_attributes_for.

+13
Jun 08 '13 at 2:45
source share

You must use has_many: through if you need validations, callbacks, or additional attributes in the join model.

+4
Dec 29 '15 at 19:01
source share

Many answers clarify that you should use has_and_belongs_to_many compared to has_many through: if you do not need any additional data or checks in the connection table.

However, beware of this approach. In the early stages of application development, it is almost impossible to find out what additional features or checks you might need in the distant future of the life cycle of your project. If you decide to use has_and_belongs_to_many and want to add one simple data assignment or verification after 2 years, transferring this change will be extremely difficult and error prone. To be safe, by default has_many :through

+1
Jul 17 '18 at 21:17
source share

Based on my experience, it is always better to use has_many: through , because at least you can add timestamps to the table. Many times during debugging of some ActiveRecord objects that are connected via HABTM, I skipped created_at , updated_at timestamps. So keep in mind that this can help you debug, investigate problems with data relationships in the context of time, because without it you are β€œblind” when the relationships were created or updated.

0
Jun 21 '19 at 11:02
source share



All Articles