How to define allow_destroy and: depend =>: destroy in Rails?

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.

Database relationship model

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?

+6
ruby-on-rails ruby-on-rails-3 dependencies nested
source share
2 answers

I think your relationship is wrong here ... accepts_nested_attributes_for should be declared on the model, that has_many for the model, that it has_many of. Also, in your example, destroying an object will provide dependent_destroy for many studies , and not vice versa.

+2
source share

You can add :dependent => :destroy to all three, but I'm not sure that it will give you enough power to perform the checks necessary to determine whether a related object should be destroyed.

You have several options.

Add a before_destroy callback for each model that throws an exception or stops deleting.

 class Facility < ActiveRecord::Base has_many :internships has_many :locations has_many :studies def before_destroy raise SomethingException if internships.any? || ... # or errors.add(... end end 

or do it silently by redefining destruction

 class Facility < ActiveRecord::Base has_many :internships has_many :locations has_many :studies def destroy return false if internships.any || ... super end end 

Note: this is mainly for guidance only and may be the wrong way to override destruction, etc.

+1
source share

All Articles