class CharacterItem < ApplicationRecord include DestroyIfZeroQuantity belongs_to :character belongs_to :item validates :quantity, presence: true after_initialize do if self.new_record? self.quantity ||= 0 end end scope :ordered_by_item_name, -> { includes(:item).order("items.name") } end