Note that dependent: :destroy
in relation to has_many :through
removes the association and unrelated record (i.e., connection records will be deleted, but the corresponding records will not). Therefore, if you delete the patient
, he will only delete the appointment
, not the physician
. Read the detailed explanation in the API docs .
I have inserted the relevant paragraphs below.
What is being deleted?
There is a potential error here: the has_and_belongs_to_many
and has_many :through
associations have entries in the connection tables, as well as related entries. So, when we call one of these removal methods, what exactly needs to be removed?
The answer is that it is assumed that the deletion in the association is related to the deletion of the relationship between the owner and the associated object (s), and not necessarily the related objects themselves. Thus, with has_and_belongs_to_many
and has_many :through
union entries will be deleted, but related entries will not.
This makes sense if you think about it: if you were to call post.tags.delete(Tag.find_by_name('food'))
, you need the food
tag to be disconnected from the post
, not the tag itself, which should be deleted from the database.
source share