When to choose between has_and_belongs_to_many and has_many, through association

This was really confusing for me when to choose has_and_belongs_to_many and when to choose has_many , through association because both sets up many-to-many relationship between tables.

Qns:  When to choose has_and_belongs_to_many association.

Ans: The answer is when we does not need any kind of information about the association or the information about join model then we should use has_and_belongs_to_many association.

Example: An assembly can have many parts and a part can have many assemblies and we don't need any other information about association then sets up direct many-to-many association between tables. via another table called assemblies_parts and it should have the foreign keys for both the table.


Qns: When to choose has_many, through association.  
Ans: The answer is when we need the information about the association or the information about join model then we should use has_many ,through association.

Example: A physician can have many patients and a patient can have many physician but a physician must have the knowledge about it's association means it's appointment and it's appointment_date with patients which drags the use of third or join model.

So it's clear when you need information about join model such as information about appointment date then you need a join model.


Comments

Popular posts from this blog

Installing Wowza Streaming Engine on ubuntu

Highcharts with grouped categories

Completely Uninstall and Install Rubymine on ubuntu