Answer the question
In order to leave comments, you need to log in
How to do calculations in the model?
There is an order model, it has a subordinate order position model.
class Order < ActiveRecord::Base
has_many :order_items, :dependent => :destroy
accepts_nested_attributes_for :order_items, :reject_if => lambda { |a| a[:inventory_id].blank? }, :allow_destroy => true
end
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :inventory
# в схеме есть свойство quantity - количество товара в строке заказа
end
class Inventory < ActiveRecord::Base
belongs_to :product
belongs_to :stock
# в схеме есть свойство amount - количество товара
end
Answer the question
In order to leave comments, you need to log in
inventory.update!(amount: inventory.amount - order_item.quantity)
class OrderCheckoutService
attr_accessor :order
def initialize(order)
@order = order
end
def checkout
ActiveRecord::Base.transaction do
# ваш код который переберет все позиции заказа, зарезервирует, сохранит, пошлет уведомления и т.п.
end
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question