Как рассчитать «итоговый» столбец для has_many: через - PullRequest
0 голосов
/ 28 июня 2019

У меня есть 3 модели

class Order < ApplicationRecord
  has_many :order_items
  has_many :items, through: :order_items
end

class OrderItem < ApplicationRecord
  belongs_to :order
  belongs_to :item
end

class Item < ApplicationRecord
  has_many :order_items
  has_many :orders, through: :order_items
end

Для этой модели была создана такая миграция

class CreateOrder < ActiveRecord::Migration[5.2]
  def change
    create_table :orders do |t|
      t.integer :user_id
      t.string :priority
      t.text :description
      t.integer :order_item_id
      t.decimal :total

      t.timestamps
    end

    create_table :items do |t|
      t.string :name
      t.string :type
      t.integer :order_item
      t.text :description
      t.decimal :cost

      t.timestamps
    end

    create_table :order_items do |t|
      t.belongs_to :order, index: true
      t.belongs_to :item, index: true
      t.integer :count

      t.timestamps
    end
  end
end

Я не понимаю, как рассчитать total для заказов?Это total = count * cost.А как использовать здесь счетчик кэша?

...