New fields added after scaffold do not persist in bank even while being strong params

1

I have a problem here that I have not resolved for 3 days. Very strange. It is in editing a record. It is only updating the first record and not the 2nd onwards. The curious thing is that this only happens with the field quatidade_dev (added after the scaffold) because quantity quantity works normal. Even including in the temporary table that is where the problem is. I already put the new fields in the strong params but it did not work.

By the console exit it seems to be catching on:

Look at the quantity_dev fields below. Both of them value 1.

Processing by OrdersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"MgDZLwYzZIp7BFd0kefREQIwpcL9g/LBKngJNdcFMNs=", "order"=>{"customer_id"=>"2", "tipo"=>"Consignado", "descontado"=>"35.0", "valor_total"=>"20.0", "item_total"=>"2", "order_num"=>"2016-37", "details_attributes"=>{"0"=>{"id"=>"356", "barcode"=>"CA001M300001000", "order_id"=>"37", "preco"=>"10.0", "cod_produto"=>"CA001", "desc_produto"=>"Calçinha teste", "cod_cor"=>"3", "desc_cor"=>"Rosa", "desc_tamanho"=>"M", "quantidade"=>"1", "quantidade_dev"=>"1", "total_dev"=>"10", "_destroy"=>"false"}, "1"=>{"id"=>"357", "barcode"=>"CA001P400001000", "order_id"=>"37", "preco"=>"10.0", "cod_produto"=>"CA001", "desc_produto"=>"Calçinha teste", "cod_cor"=>"4", "desc_cor"=>"Azul", "desc_tamanho"=>"P", "quantidade"=>"1", "quantidade_dev"=>"1", "total_dev"=>"10", "_destroy"=>"false"}}}, "commit"=>"Salvar", "id"=>"37"}

But when I try to insert them into a temporary table, one hour it passes the 14 parameters (1st register) and then it only passes 12 (2nd register).

INSERT INTO "tmps" ("barcode", "cod_cor", "cod_produto", "created_at", "desc_cor", "desc_produto", "desc_tamanho", "order_id", "preco", "quantidade", "quantidade_dev", "total", "total_dev", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING "id" [["barcode", "CA001M300001000"], ["cod_cor", "3"], ["cod_produto", "CA001"], ["created_at", "2016-09-21 15:00:26.000000"], ["desc_cor", "Rosa"], ["desc_produto", "Calçinha teste"], ["desc_tamanho", "M"], ["order_id", "37"], ["preco", "10.0"], ["quantidade", 1], ["quantidade_dev", 1], ["total", 10.0], ["total_dev", "10.0"], ["updated_at", "2016-09-21 15:00:26.000000"]]

INSERT INTO "tmps" ("barcode", "cod_cor", "cod_produto", "created_at", "desc_cor", "desc_produto", "desc_tamanho", "order_id", "preco", "quantidade", "total", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["barcode", "CA001P400001000"], ["cod_cor", "4"], ["cod_produto", "CA001"], ["created_at", "2016-09-21 15:00:26.000000"], ["desc_cor", "Azul"], ["desc_produto", "Calçinha teste"], ["desc_tamanho", "P"], ["order_id", "37"], ["preco", "10.0"], ["quantidade", 1], ["total", 10.0], ["updated_at", "2016-09-21 15:00:26.000000"]]

If after the save I put a debug point, the values are not really saved in the details table (only the 1st record is saved, the 2nd is not) If I go back and change the 2nd record and have it saved, save both But being empty he does not save the others, only the first.

Could someone help?

I put the controllers and forms in this gist:

link

Follow the models:

Order.rb

class Order < ActiveRecord::Base
    has_many :details, dependent: :destroy
    belongs_to :customer
    accepts_nested_attributes_for :details, reject_if: proc { |attributes| attributes['cod_produto'].blank? }, :allow_destroy => true
    validates :customer_id,
            :presence => true
    after_validation :sum_details
  # after_save :update_quantity
    after_create :set_order_num
  before_validation :set_percentage
  # after_commit :agrupa_detalhes
  # after_save :update_quantity

    TIPO = ['Venda',
    'Consignado',
    ]

    validates :tipo,
              inclusion: { in: TIPO }
    validates :tipo, presence:true
    validates :descontado,
            :presence => true,
            :numericality => true
    def valor_recebido
        if valor_total_dev.present?
            valor = ((valor_total - valor_total_dev) - (((valor_total - valor_total_dev) * descontado) / 100))
        else
            valor_total - ((valor_total * descontado)/100)
        end
    end
    def qtd_final
        if item_total_dev.present?
            qtd = item_total - item_total_dev
        else
            item_total
        end
    end
   private
   def update_quantity
    self.details.each do |d|
      # Atualiza estoque
      search = d.barcode
      variation = Variation.find_by(barcode:search)
      if d.quantidade_dev.present?
       qtd_detail = variation.quantity + d.quantidade_dev
       variation.quantity = qtd_detail
      elsif d.quantidade.present?
       qtd_detail = variation.quantity - d.quantidade
       variation.quantity = variation.quantity - d.quantidade
      end
      variation.save!
    end
   end

   def set_percentage
    if self.tipo == "Consignado"
      self.descontado = 35
    else
      self.descontado = 50
    end
   end
   def set_order_num
    update(order_num: "#{Date.current.year}-#{self.id}")

   end
   def sum_details
     total = 0
     qtd = 0
     total_dev = 0
     qtd_dev = 0
     self.details.each do |d|
      # Acumula valores
      total += (d.quantidade * d.preco) if d.quantidade.present?
      qtd += d.quantidade if d.quantidade.present?
      total_dev += (d.quantidade_dev * d.preco) if d.quantidade_dev.present?
      qtd_dev += d.quantidade_dev if d.quantidade_dev.present?

     end
     self.valor_total = total
     self.item_total = qtd
     self.valor_total_dev = total_dev if total_dev != 0
     self.item_total_dev = qtd_dev if qtd_dev != 0

   end
end

Detail.rb

class Detail < ActiveRecord::Base

    belongs_to :order
  before_save :calcula_totais
  after_save :insere,:update_quantity
    def valor_recebido
      if total_dev.present?
        valor = ((total - total_dev) - (((total - total_dev) * self.order.descontado) / 100))
      else
        total - ((total * self.order.descontado)/100)
      end
    end


    private
  def insere
      o = self.order
      # debugger
        Tmp.destroy_all
        @detail = o.details.select("order_id,cod_produto,desc_produto,cod_cor,desc_tamanho,desc_cor,barcode, preco ,sum(quantidade) as quantidade, sum (total) as total,sum(quantidade_dev) as quantidade_dev, sum(total_dev) as total_dev").group("order_id,cod_produto,desc_produto,cod_cor,desc_cor,desc_tamanho,barcode,preco").order("cod_produto asc, desc_tamanho asc, desc_cor asc").flatten
        ActiveRecord::Base.transaction do
          @detail.each do |detail|
            time_created_at = Time.now.to_s(:db)
            time_updated_at = Time.now.to_s(:db)
            Tmp.create(order_id: detail.order_id,cod_produto: detail.cod_produto,desc_produto: detail.desc_produto,cod_cor: detail.cod_cor,desc_tamanho: detail.desc_tamanho,desc_cor: detail.desc_cor,preco: detail.preco,quantidade: detail.quantidade,created_at: time_created_at,updated_at: time_updated_at,total: detail.total,quantidade_dev: detail.quantidade_dev,total_dev: detail.total_dev,barcode: detail.barcode)
          end
        end
        o.details.destroy_all
        detail_items = Tmp.pluck(:order_id,:cod_produto,:desc_produto,:cod_cor,:desc_cor,:desc_tamanho,:preco,:quantidade,:total,:quantidade_dev,:total_dev,:barcode)
        Detail.import([:order_id,:cod_produto,:desc_produto,:cod_cor,:desc_cor,:desc_tamanho,:preco,:quantidade,:total,:quantidade_dev,:total_dev,:barcode], detail_items)
    end
    def get_quantity
        @qtd = self.quantidade
    end
  def calcula_totais
     if quantidade_dev.present?
      total_dev = quantidade_dev * preco
     else
      quantidade_dev = 0
     end
     if quantidade.present?
      total = quantidade * preco
     end
  end
    def update_quantity
        search = barcode
        variation = Variation.find_by(barcode:search)
         if quantidade_dev.present?
            qtd = variation.quantity + quantidade_dev
            variation.quantity = qtd
         elsif quantidade.present?
            qtd = variation.quantity - quantidade
            variation.quantity = variation.quantity - quantidade
         end
        variation.save!
    end
end
    
asked by anonymous 22.09.2016 / 00:50

0 answers