title: How do I maintain associations for a deleted object?
body: <p>I have an object <code>PortStock</code>, which I keep track of via <code>OrderHistory</code>. i.e. every time a specific action is taken on a <code>port_stock</code> object, I create an <code>order_history</code> object associated with that <code>port_stock</code>.</p>
  
  <p>Think of <code>OrderHistory</code> as a transaction history on that specific <code>PortStock</code>.</p>
  
  <p>The issue now is, when I delete a <code>PortStock</code> object, I still want to be able to retrieve (and display) all the <code>order_history</code> objects associated with that <code>PortStock</code> record.</p>
  
  <p>This is how I currently do it, which doesn't work when <code>@port_stock</code> has been deleted:</p>
  
  <pre><code>@port_stock = current_user.port_stocks.friendly.find(params[:id])
  @stock = @port_stock.stock
  @order_histories = OrderHistory.joins(:port_stock).where(port_stocks: { ticker: @stock.ticker}).group_by { |o| o.port_stock }
  </code></pre>
  
  <p>Thoughts?</p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>My <code>PortStock</code> model looks like this:</p>
  
  <pre><code># == Schema Information
  #
  # Table name: port_stocks
  #
  #  id                :bigint(8)        not null, primary key
  #  portfolio_id      :integer
  #  stock_id          :integer
  #  volume            :integer
  #  action            :integer
  #  position          :integer          default("open")
  #  ticker            :string
  #
  
  class PortStock &lt; ApplicationRecord
    extend FriendlyId
    friendly_id :ticker
    belongs_to :portfolio
    belongs_to :stock
    has_many :order_histories
  
    enum action: [ :buy, :sell ]
    enum position: [ :open, :closed ]
  end
  </code></pre>
  
  <p>My <code>OrderHistory</code> model looks like this:</p>
  
  <pre><code># == Schema Information
  #
  # Table name: order_histories
  #
  #  id                :bigint(8)        not null, primary key
  #  port_stock_id     :integer
  #  num_units         :integer
  #  action            :integer
  #  created_at        :datetime         not null
  #  updated_at        :datetime         not null
  #
  
  class OrderHistory &lt; ApplicationRecord
    belongs_to :port_stock
    enum action: [ :buy, :sell ]
  end
  </code></pre>