class Transfer < ActiveRecord::Base audited belongs_to :origin, class_name: 'Pointsale' belongs_to :destiny, class_name: 'Pointsale' belongs_to :user, class_name: 'User' belongs_to :received_by, class_name: 'User' has_many :transfer_details has_many :products, through: :transfer_details has_many :inventories_moves enum status: [:cancelled, :pending, :received] accepts_nested_attributes_for :transfer_details validates_associated :transfer_details validates :origin_id, presence: { message: "Debe indicar origen del traspaso." }, on: [:create, :update] validates :destiny_id, presence: { message: "Debe indicar destino del traspaso." }, on: [:create, :update] validates :received_by_id, presence: { message: "Debe indicar quien recibió el traspaso." }, on: [:update] validate :require_products, on: :create scope :activos, -> { where(status: 1) } private def require_products errors.add(:base, "Debe seleccionar al menos un producto") if transfer_details.size == 0 end end