代码之家  ›  专栏  ›  技术社区  ›  marcamillion blelump

如何维护已删除对象的关联?

  •  1
  • marcamillion blelump  · 技术社区  · 7 年前

    我有个目标 PortStock ,我一直在跟踪via OrderHistory port_stock 对象,我创建一个 order_history 与之关联的对象 港口股票 .

    港口股票

    港口股票 对象,我仍然希望能够检索(并显示)所有 订单历史 港口股票

    我现在就是这样做的,但当 @port_stock 已删除:

    @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 }
    

    思想?

    编辑1

    我的

    # == 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 < 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
    

    我的 订单历史记录

    # == 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 < ApplicationRecord
      belongs_to :port_stock
      enum action: [ :buy, :sell ]
    end
    
    0 回复  |  直到 7 年前
    推荐文章