我有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
。以及如何在这里使用计数器缓存?