Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck Before Destroy or Destroy all Associated? Rails 4
    primarykey
    data
    text
    <p>I currently have a Vendor Model which acts as sort of a router for my users. This Vendor Model links up to locations that the vendor owns and campaigns as well. </p> <p>Once a user is created, it is given a Vendor association which basically unlocks the locations and campaigns that pertain to that given Vendor.</p> <p>Now, my question is, is there a way that when a Vendor is "Destroyed" that is can destroy all associated locations, campaigns and users linked to it? If not, is there a way to run a validation that if a Vendor is being destroyed to check the database for associations and not allow the action of destroy if it finds records?</p> <p>The reason I ask is because if I destroy the Vendor, it leaves the users without locations and campaigns without an associated Vendor which basically crashes my application.</p> <p><strong>User Model</strong></p> <pre><code>belongs_to :vendor #user can only have one vendor has_many :locations, :through =&gt; :vendor has_many :clients, :through =&gt; :vendor has_many :campaigns, :through =&gt; :vendor </code></pre> <p><strong>Location Model</strong></p> <pre><code>belongs_to :vendor has_many :campaigns </code></pre> <p><strong>Campaign Model</strong></p> <pre><code>belongs_to :location belongs_to :user belongs_to :vendor has_one :client </code></pre> <p><strong>Vendor Model</strong></p> <pre><code>has_many :locations #vendor can own many locations has_many :clients #vendor can have many clients. Allows multiple employees to see client list has_many :campaigns #vendor can have many campaigns. Allows multiple employees to see campaigns </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload