Has_many through association-dependent destruction, provided that anyone called destroy

Is there a way to check inside before_destroy hook which object (class) is called destroy ?

In the following example, when a patient destroyed, their appointments are also found (this is what I want); however, I do not want the physician be destroyed if there are appointments associated with this physician .

Again, is there a way to do such a check in the before_destory ? If not, is there another way to perform this “kill check” based on the “direction” of the call (i.e. based on who called)?

 class Physician < ActiveRecord::Base has_many :appointments, dependent: :destroy has_many :patients, through: :appointments end class Patient < ActiveRecord::Base has_many :appointments, dependent: :destroy has_many :physicians, through: :appointments end class Appointment < ActiveRecord::Base belongs_to :patient belongs_to :physician before_destroy :ensure_not_referenced_by_anything_important private def ensure_not_referenced_by_anything_important unless patients.empty? errors.add(:base, 'This physician cannot be deleted because appointments exist.') false end end end 
+6
source share
2 answers

Just say:

 class Physician < ActiveRecord::Base has_many :appointments, dependent: :restrict_with_exception has_many :patients, through: :appointments end 

Note the dependent: :restrict_with_exception . This will force Active Record to refuse to destroy any doctor records that have associated Record appointments.

See the API docs and association basics guide .

+11
source

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.

+15
source

Source: https://habr.com/ru/post/912567/


All Articles