class ProductOrder include SimplyStored::Couch include ActiveModel::SerializerSupport property :quantity, type: Integer property :price, type: Float property :product_name property :product_variant belongs_to :product belongs_to :order view :by_product_id, key: :product_id view :by_order_id, key: :order_id # Getter for product name. If a supplier deletes a product, that has product_orders, the product # will become nil. This method should handle this case. alias_method :direct_product_name, :product_name def product_name direct_product_name.presence || product.try(:name) || '[deleted]' end end