Есть контроллер №1, он создает запрос, который отправляет значение в другой контролер
def create
@s_transaction = STransaction.new(s_transaction_params)
authorize @s_transaction
respond_to do |format|
if @s_transaction.save
format.html { redirect_back_or_default s_transactions_url, t('Record has
been saved') }
format.json { render :show, status: :created, location: @s_transaction }
else
format.html {
@s_transaction.s_transaction_materials.build if
@s_transaction.s_transaction_materials.blank?
@s_transaction.s_transaction_tools.build if
@s_transaction.s_transaction_tools.blank?
@s_transaction.s_transaction_others.build if
@s_transaction.s_transaction_others.blank?
@s_transaction.s_transaction_additionals.build if
@s_transaction.s_transaction_additionals.blank?
render :new
}
format.json { render json: @s_transaction.errors, status:
:unprocessable_entity }
end
В контролере №2 идет расчет
def show
authorize @facility
@facility_note = FacilityNote.new(facility: @facility)
set_return_to
@return_to = get_return_to_or_default facilities_url
@additional_list = Array.new
@material_list = Array.new
@other_list = Array.new
@tool_list = Array.new
income_transactions = STransaction.in_facility(@facility).to_a
income_transactions.each { |transaction|
transaction.s_transaction_additionals.each { |additional|
idx = @additional_list.find_index {|obj| obj.s_additional.id == additional.s_additional.id}
if idx.blank?
@additional_list << additional
else
@additional_list[idx].s_amount += additional.s_amount
end
}
transaction.s_transaction_materials.each { |material|
idx = @material_list.find_index {|obj| obj.s_material.id == material.s_material.id}
if idx.blank?
@material_list << material
else
@material_list[idx].s_amount += material.s_amount
end
}
transaction.s_transaction_others.each { |other|
idx = @other_list.find_index {|obj| obj.s_other.id == other.s_other.id}
if idx.blank?
@other_list << other
else
@other_list[idx].s_amount += other.s_amount
end
}
transaction.s_transaction_tools.each { |tool|
@tool_list << tool
}
}
outcome_transactions = STransaction.out_facility(@facility).to_a
outcome_transactions.each { |transaction|
transaction.s_transaction_additionals.each { |additional|
idx = @additional_list.find_index {|obj| obj.s_additional.id == additional.s_additional.id}
unless idx.blank?
@additional_list[idx].s_amount -= additional.s_amount
end
}
transaction.s_transaction_materials.each { |material|
idx = @material_list.find_index {|obj| obj.s_material.id == material.s_material.id}
unless idx.blank?
@material_list[idx].s_amount -= material.s_amount
end
}
transaction.s_transaction_others.each { |other|
idx = @other_list.find_index {|obj| obj.s_other.id == other.s_other.id}
unless idx.blank?
@other_list[idx].s_amount -= other.s_amount
end
}
transaction.s_transaction_tools.each { |tool|
idx = @tool_list.find_index {|obj| obj.s_tool.id == tool.s_tool.id}
unless idx.blank?
@tool_list.delete_at(idx)
end
}
}
@additional_list = @additional_list.reject {|o| o.s_amount == 0}
@material_list = @material_list.reject {|o| o.s_amount == 0}
@other_list = @other_list.reject {|o| o.s_amount == 0}
end
Вопрос, как сделать так чтобы из запроса нельзя было получить отрицательное число ? Я побывал делать условие в расчете самого значения
transaction.s_transaction_materials.each { |material|
idx = @material_list.find_index {|obj| obj.s_material.id == material.s_material.id}
unless idx.blank?
@material_list[idx].s_amount -= material.s_amount
$per = 0
if @material_list[idx].s_amount < 0 then
$per = 1
else $per 0
end
end
}
и в контроллер №1 добавлял это
if $per = 0 then
@s_transaction.save
else
@s_transaction.destroy
но это работает только в половине случаев, так как сначала выполняется действие в контроллере №1 и только потом идет проверка в контроллере №2, как это можно исправить?