title: RoR associations: has_one + has_many depedent: :destroy
body: <p>I don't get why I'm having orphans records when I try to destroy a user. A <strong>User</strong> <em>has one</em> <strong>Cart</strong> which <em>has many</em> <strong>CartItem</strong>
  <hr>
  <strong>User has one cart:</strong></p>
  
  <pre class="lang-rb prettyprint-override"><code>class User &lt; ApplicationRecord
    has_one :cart, dependent: :destroy
    has_many :cart_items, through: :cart, dependent: :destroy
    has_many :samples, through: :cart_items, source: :cartable, source_type: 'Sample'
    has_many :tracks, through: :cart_items, source: :cartable, source_type: 'Track'
  end
  </code></pre>
  
  <blockquote>
    <p>:dependent</p>
    
    <p>Controls what happens to the associated object when its owner is destroyed:</p>
    
    <ul>
    <li>:destroy causes the associated object to also be destroyed</li>
    </ul>
    
    <p><a href="https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_one" rel="nofollow noreferrer">https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_one</a></p>
  </blockquote>
  
  <p><strong>Cart has many items:</strong></p>
  
  <pre class="lang-rb prettyprint-override"><code>class Cart &lt; ApplicationRecord
    belongs_to :user
    has_many :cart_items, dependent: :destroy
    has_many :samples, through: :cart_items, source: :cartable, source_type: 'Sample'
    has_many :tracks, through: :cart_items, source: :cartable, source_type: 'Track'
  end
  </code></pre>
  
  <blockquote>
    <p>:dependent</p>
    
    <p>Controls what happens to the associated objects when their owner is destroyed. Note that these are implemented as callbacks, and Rails
    executes callbacks in order. Therefore, other similar callbacks may
    affect the :dependent behavior, and the :dependent behavior may affect
    other callbacks.</p>
    
    <ul>
    <li>:destroy causes all the associated objects to also be destroyed.</li>
    </ul>
    
    <p><a href="https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_many" rel="nofollow noreferrer">https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_many</a></p>
  </blockquote>
  
  <p><strong>And items:</strong></p>
  
  <pre class="lang-rb prettyprint-override"><code>class CartItem &lt; ApplicationRecord
    belongs_to :cart
    belongs_to :cartable, polymorphic: true
  end
  </code></pre>
  
  <p><hr>
  I'd like to be able to destroy a User with  for example <code>User.last.destroy</code>, but instead I've an error:</p>
  
  <pre><code>ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  update or delete on table "users" violates foreign key constraint "fk_rails_ea59a35211" on table "carts"
  DETAIL:  Key (id)=(227) is still referenced from table "carts".
  </code></pre>
  
  <hr>
  
  <p>I was thinking that <code>has_one :cart, dependent: :destroy</code> would do the job but it looks like I'm wrong. What am I missing ? </p>
  
  <p>Thanks for your time</p>