Given the following database model, how and where do you define the deletion relationship between models? I figured out the basic table association setting, but when I want to add dependencies to enable the removal of nested objects , I get lost.

Here is the relationship model I created.
class User < ActiveRecord::Base has_many :studies end class Study < ActiveRecord::Base has_many :internships belongs_to :student, :class_name => "User", :foreign_key => "user_id" belongs_to :subject belongs_to :university, :class_name => "Facility", :foreign_key => "facility_id" accepts_nested_attributes_for :subject, :university, :locations end class Subject < ActiveRecord::Base has_many :studies end class Internship < ActiveRecord::Base belongs_to :study belongs_to :company, :class_name => "Facility", :foreign_key => 'facility_id' accepts_nested_attributes_for :company, :study end class Facility < ActiveRecord::Base has_many :internships has_many :locations has_many :studies accepts_nested_attributes_for :locations end class Location < ActiveRecord::Base belongs_to :facility end
Where would you put :dependent => :destroy and :allow_destroy => true to include the following scripts? I do not want to confuse you. Therefore, I give up my attempts.
Internship scenario: User wants to delete internship.
- A company (facility) associated with it may be deleted if the company is not associated with another internship.
- If so, the locations of the associated company may be deleted.
- Related research will not be affected.
Study scenario . The user wants to delete the study.
- An object associated with it may be deleted if no other research is relevant to this issue.
- A university (object) associated with it can be deleted if no other research is related to this university.
- His associate internships may be deleted. A company can only be deleted if another internship does not apply to it.
I am completely unsure if I can add :dependent => :destroy only after has_one and has_many , and also after belongs_to .
Edit: To simplify the problem, stick to the following (given) implementation of the example.
class Study < ActiveRecord::Base belongs_to :subject accepts_nested_attributes_for :subject, :allow_destroy => true end class Subject < ActiveRecord::Base has_many :studies, :dependent => :destroy end
In my opinion, I provide the following link.
<%= link_to "Destroy", study, :method => :delete, :confirm => "Are you sure?" %>
The path is based on named routes specified in routes.rb configuration.
resources :studies resources :subjects
The study will be deleted when I click the link - the items remain intact. Why?